Hi!

On 2017-02-08, valerio...@gmail.com <valerio...@gmail.com> wrote:
> Is there a difference between 
> return expand(f^2)
> and
> return (g^2).expand()
> or are they perfect synonyms?

SageMath's language is Python, in particular it is object oriented.
Personally, I'd always prefer calling a method of an object (that's to
say (g^2).expand()) over calling a function that is defined in the
global namespace (that's to say expand(g^2)).

Reasons:
- If you have a function F in the global namespace and some object x,
  how would you know that applying F to x makes any sense? If, on the
  other hand, you have a method x.f of x, then you know that it makes
  sense.
- Do you know how to get information in Python (and thus in SageMath)
  by "tab completion"? If you have an object x and start to type
     x.
  and then hit the <tab> key, you will see a list of all methods
  available for x. (And if you start typing x.e and then hit the <tab>
  key, the list will only comprise methods whose name starts with e).
  This list will be a lot more concise then the list of all functions in
  the global namespace.
- A function such as "expand" will normally just call a method anyway.
  Do you know how to access the source code in Python? Just put a
  question mark after an object x and hit return (resp. shift-return in
  the SageMath notebook) -- you will see the documentation of x. Or put
  two question marks after x, and you will see the source code.

  Do this with the "expand" function. The result is (basically) this:

  sage: expand??
  Signature: expand(x, *args, **kwds)
  Source:   
  def expand(x, *args, **kwds):
      """
      EXAMPLES::
      ...
      """
      try:
          return x.expand(*args, **kwds)
      except AttributeError:
          return x

So, you can see from the code that they basically are synonymous, except
that that "expand" function just returns x if x happens to not have an
"expand" method.

> The same question for  
> lambda f: (f^2).expand()
> (in Simon's answer): is the lambda construction just a shortcut, equivalent 
> to 
> return (g^2).expand()
> or is it something different?

That's again a question on Python. Both ways give you an object of the
same type:

  sage: def f_1(x):
  ....:     return 2*x
  ....:
  sage: f_2 = lambda x: 2*x
  sage: type(f_1)
  <type 'function'>
  sage: type(f_1) is type(f_2)
  True

I guess from a theoretical point of view ("lambda calculus"), it is
possible to do everything using the "lambda" construction. However, as
soon as it gets a little more complicated, "def" constructions are
easier to read.

Best regards
Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to