Opened 7 years ago

Closed 7 years ago

#13586 closed defect (fixed)

BuiltinFunction expects to be instantiated only once

Reported by: burcin Owned by: burcin
Priority: major Milestone: sage-5.5
Component: symbolics Keywords: symbolic function
Cc: Merged in: sage-5.5.beta0
Authors: Burcin Erocal Reviewers: Karl-Dieter Crisman
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by kcrisman)

From this ask.sagemath.org question:

sage: from sage.symbolic.function import BuiltinFunction
sage: class AFunction(BuiltinFunction):                            
....:         def __init__(self, name, exp=1):
....:             self.exponent=exp
....:         BuiltinFunction.__init__(self, name, nargs=1)
....:     def _eval_(self, arg):
....:             return arg**self.exponent
....: 
sage: p2 = AFunction('p2', 2)
sage: p2(x)                  
x^2
sage: p3 = AFunction('p3', 3)
sage: p3(x)                  
x^2

ATM, we only allow a BuiltinFunction to be instantiated once.


Apply trac_13586-builtin_function.patch.

Attachments (1)

trac_13586-builtin_function.patch (1.8 KB) - added by burcin 7 years ago.

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by burcin

comment:1 Changed 7 years ago by burcin

  • Status changed from new to needs_review

comment:2 follow-up: Changed 7 years ago by kcrisman

  • Description modified (diff)
  • Reviewers set to Karl-Dieter Crisman
  • Status changed from needs_review to positive_review

Nice work, Burcin. I guess this doesn't stop someone from doing

sage: p4 = AFunction('p2',3)

but I suppose that is the user's problem, since they are redefining the name p2. Similarly, is this (new behavior, I think) ok? I think so, but just want to make sure.

sage: class MyFunction(BuiltinFunction):
....:     def __init__(self,name):
....:         self.exponent=1
....:         BuiltinFunction.__init__(self,name,nargs=1)
....:     def _eval_(self,arg):
....:         return arg**self.exponent
....:     
sage: MyFunction('p')
p
sage: p = MyFunction('p')
sage: p
p
sage: p(2)
2
sage: p(3)
3
sage: q = MyFunction('q')
sage: q(3)
3
sage: p
p
sage: q
q

Patchbot: Apply trac_13586-builtin_function.patch

comment:3 in reply to: ↑ 2 Changed 7 years ago by burcin

Thanks for the quick review.

Replying to kcrisman:

Similarly, is this (new behavior, I think) ok? I think so, but just want to make sure.

sage: class MyFunction(BuiltinFunction):
....:     def __init__(self,name):
....:         self.exponent=1
....:         BuiltinFunction.__init__(self,name,nargs=1)
....:     def _eval_(self,arg):
....:         return arg**self.exponent
....:     
sage: MyFunction('p')
p
sage: p = MyFunction('p')
sage: p
p
sage: p(2)
2
sage: p(3)
3
sage: q = MyFunction('q')
sage: q(3)
3
sage: p
p
sage: q
q

I think this is how it used to work without the patch as well. But I admit that I don't understand your question. Are you surprised that q prints as q not p? We always use the user supplied name when printing the instance object. It's a different matter for the self argument you have inside the _eval_() function.

comment:4 Changed 7 years ago by kcrisman

No, it's just that you said "ATM, we only allow a BuiltinFunction? to be instantiated once." Whereas now it can be instantiated more than once with or without different arguments etc. Probably this is ok, I just wanted to be sure. No problem.

comment:5 Changed 7 years ago by jdemeyer

  • Merged in set to sage-5.5.beta0
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.