How to find a list of all ** args functions?

How can I find a list of all **args for a function?

For example, I know that symbols() accepts positive=True , real=True , etc. as arguments, and I would like to see a complete list of these possible arguments. However, they are not listed on the Symper Core doc page.

And I myself dug the source code myself, but I can’t track and find what I’m looking for.

+4
source share
4 answers

symbols() function

As other answers noted - one use of **args in symbols is to convey in the Assumptions about the created Symbol . The list of assumptions you can make is documented on the Assumptions page as supported predicates .

However , you should also note that some other special named arguments may be passed.

They are both documented in the section that you link and are in:

  • cls=<ClassName>

    Despite their name, symbols () can create objects similar to symbols, for example, instances of the Function or Wild classes. To do this, specify the cls keyword argument for the type you want:

    NB If not specified, the default Symbol class is used.

  • seq=<True|False>

    The docs say:

    If an iterable container is required for a single character, set the seq argument to True or the end of the symbol name with a comma

Code walk

You noticed that you looked at the code, so I will show you where they are implemented in the code. If you call the symbols() function, it performs various checks on its arguments, including pop -ing cls and seq from **args it then performs more checks, etc., before finally accessing the Symbol instance here , here or here . They invoke the Symbol constructor (or its subclass passed through cls ), with what remains in **args , which are all interpreted as assumptions in the constructor. - they are sanitized here , i.e. invalid or not applicable named arguments are thrown out at this point!

This shows that Assumptions + cls + seq form a set of named arguments that can be passed to **args before symbols()


Other functions (general case)

It seems to me that symbols() can simply serve as a representative example of a more general question. I hope that the above convinces you that all the values ​​that can be used with value in symbols() are documented. This can give you some confidence that the same is true for other features in SymPy.

However, in the general case, the answer is that it is rather difficult to prove to yourself that all the values ​​that can be passed as keywordarguments are contained in the library documentation or the any function. Indeed, sometimes only a subset is documented, since they are the “public API” for the library, while the actual code may take other arguments, but for some reason the developer does not want to publish them to the public. because their availability may change or their functionality has not been tested.

If you pass invalid arguments, the behavior of the library used may be different. Some libraries or functions will ignore them, while others will throw errors if you pass invalid keyword arguments.

If you want to know if this case (and the library is open source, for example, SymPy), you can always dive into the code (as I will show in Code Walk above). If you do this, you need to follow the path of execution, looking for args.pop() . If SymPy has other features that bother you, let me know in the comments, but hopefully this general method will work for you.

I assume that you understand the syntax of *args and **args . If this is not entirely clear to you, this section of the official putoron tutoria deals with this.

+3
source

These arguments are called assumptions and can be found in the documentation: Sympy assumptions

+3
source

Classification of Assumptions

  • associated with characters for simplicity, for example. Q.positive , Q.even .
  • associated with algebraic fields / rings, e.g. Q.real , Q.complex .
  • related to some facts, for example .. is_bounded , is_infinity , is_zero and soon. They help us work with core calculations. Apparently, they are obtained from the above classes of assumptions (when starting objects) (in this case, for example, is_zero means that this is Zero for the ring). Or they can be obtained from the analysis of expressions: in this case we can create some class of calculations (in this case, is_zero can be, but this calculation is a complex so-called “zero test”). In any case, we can understand that we mean exactly (now in the kernel somewhere we used is_zero in the second sense).

Assumptions Examples:

 M ... Mathematica S0 ... SymPy, current approach S1 .... SymPy, approach 1 S2 .... SymPy, approach 2 M: Simplify[1/Sqrt[x] - Sqrt[1/x], x > 0] S1: x = Symbol('x', assumptions=IsPositive) simplify(1/sqrt(x) - sqrt(1/x)) S2: simplify(1/sqrt(x) - sqrt(1/x), Assumptions(x>0)) M: FunctionExpand[Log[xy], x > 0 && y > 0] S1: x, y = Symbol('x', assumptions=IsPositive), Symbol('y', assumptions=IsPositive) log(x*y).expand() S2: log(x*y).expand(Assumptions([x>0, y>0])) M: Simplify[Sin[n Pi], n \[Element] Integers] S1: n = Symbol('n', assumptions=IsInteger) simplify(sin(n*pi)) S2: simplify(sin(n*pi), Assumptions(Element(n, Integer))) # we can talk about the syntax of Element(n, Integer) M: FunctionExpand[EulerPhi[mn], {m, n} \[Element] Integers && GCD[m, n] == 1] S1: n = Symbol('n', assumptions=IsInteger) m = Symbol('m', assumptions=IsInteger) n.assumptions.add(Eq(gcd(m, n) - 1)) euler_phi(m, n) S2: euler_phi(m, n).expand(Assumptions([Element(n, Integer), Element(m, Integer), Eq(gcd(m, n) - 1)])) # again we can talk about the syntax of Element(n, Integer) M: RealQ[x, x \[Element] Real] S0: x = Symbol('x',real=True, integer=True) assert x.is_real == True S1: S2: assert IsElement(x, Real, assumptions=Element(x, Real)) M: Refine[Abs[x], x>0] Refine[Abs[x], x0)) print e.refine(Assumptions(x)) 

Other links:

Wiki Sympi Assumptions

Assuming

Setting variable assumptions in Sympy relative to other variables

Using New SymPy Assumptions

+3
source

You can get argument details from any function using the python built-in library called inspect :

 import inspect inspect.getargspec(funcname) 

It will return an ArgSpec named tuple with some information, for example:

 ArgSpec(args=['myarg'], varargs=None, keywords=None, defaults=(None,)) 

To get the argument names, you can simply access the args attribute of this returned object.

+1
source

All Articles