What is the other type that is different from FunctionType of a simplify?

22 views
Skip to first unread message

Ruchit Vithani

unread,
Oct 30, 2018, 3:34:53 PM10/30/18
to sympy
I was looking code of rref() function of matrixes.py, In this function, there is a function variable defined as 

simpfunc = simplify if isinstance(
simplify, FunctionType) else _simplify

Why here it is checked that simplify is a FunctionType, which other type does the simplify word has? 
Also at the beginning of the matrices.py following is imported

from sympy.simplify import simplify as _simplify, nsimplify

why simplify is imported as _simplify as well as nsimplify? I mean what is the need of both?

 

Aaron Meurer

unread,
Oct 30, 2018, 4:17:32 PM10/30/18
to sy...@googlegroups.com
Your confusions here relate to Python's syntax.

simplify is defined by the keyword argument to the rref() function:

def rref(self, iszerofunc=_iszero, simplify=False, pivots=True,
normalize_last=True):

The default is False, unless the user specifies simplify=<something
else>, where <something else> should be a function.

Actually this code is not written in a very idiomatic way. The default
False implies that no simplification should happen, but this is not
the case. A better way to write this code would be

simpfunc = simplify or _simplify

with the function definition as

def rref(self, iszerofunc=_iszero, simplify=None, pivots=True,
normalize_last=True):

The isinstance(simplify, FunctionType) type check is not necessary.

> why simplify is imported as _simplify as well as nsimplify? I mean what is the need of both?

The simplify() function is imported as _simplify because otherwise it
would not be accessible in rref(), because the name "simplify" is
already used by the keyword argument.

nsimplify() is a separate function. The syntax

from sympy.simplify import simplify as _simplify, nsimplify

should be read as

from sympy.simplify import (simplify as _simplify), nsimplify # This
is not valid syntax, but this is how the precedence works

or equivalently,

from sympy.simplify import simplify as _simplify
from sympy.simplify import nsimplify

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/8cf34aa0-bdaa-4fb3-a0b3-cdccfb731206%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ruchit Vithani

unread,
Oct 30, 2018, 4:41:56 PM10/30/18
to sympy

Should I go and fix this?


simpfunc = simplify or _simplify

with the function definition as

def rref(self, iszerofunc=_iszero, simplify=None, pivots=True,
normalize_last=True):

The isinstance(simplify, FunctionType) type check is not necessary.

This was silly! Actually I don't think I have seen such syntax before, so this happened, and also I didn't know what to google in this case. I tried to google, but got other awful stuff :(

Aaron Meurer

unread,
Oct 30, 2018, 4:51:38 PM10/30/18
to sy...@googlegroups.com
On Tue, Oct 30, 2018 at 2:41 PM Ruchit Vithani <vithan...@gmail.com> wrote:
>
>
> Should I go and fix this?

Yes you can if you want.

>>
>>
>> simpfunc = simplify or _simplify
>>
>> with the function definition as
>>
>> def rref(self, iszerofunc=_iszero, simplify=None, pivots=True,
>> normalize_last=True):
>>
>> The isinstance(simplify, FunctionType) type check is not necessary.
>>
> This was silly! Actually I don't think I have seen such syntax before, so this happened, and also I didn't know what to google in this case. I tried to google, but got other awful stuff :(

The docs on the "import" statement describe "import ... as ..."
https://docs.python.org/3/reference/simple_stmts.html#import.
Unfortunately, they also go into a lot of details on things that
aren't important (the Python import system is quite complex and the
vast majority of things you will never need to care about).

I suppose the section in the tutorial is better for this, as it
doesn't go into unnecessary details
https://docs.python.org/3/tutorial/modules.html#more-on-modules

Aaron Meurer

>>
>> > why simplify is imported as _simplify as well as nsimplify? I mean what is the need of both?
>>
>> The simplify() function is imported as _simplify because otherwise it
>> would not be accessible in rref(), because the name "simplify" is
>> already used by the keyword argument.
>>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/da579ebd-bfd5-4c83-ad22-1fd6de7249ed%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages