| | |
- ex
- test_01_ex
- test_02_symbol
- test_03_numeric
- test_04_lst
- test_05_add
- test_06_mul
- test_07_power
- test_08_inifcns
- test_09_function
- test_10_relational
- test_11_seq
- test_12_constant
- test_13_matrix
- test_14_pseries
- test_15_ncmul
class ex |
| |
|
| |
- __add__(self, other)
- __cmp__(self, other)
- Return `numeric(self-other).csgn()'.
If `self-other' is not ex(numeric), ValueError is raised
about indeterminate result.
- __coerce__(self, other)
- Return tuple (self, ex(other)).
- __complex__(self)
- `self' must be ex(numeric).
- __delitem__(self, index)
- `self' must be ex(lst) [not implemented].
- __div__(self, other)
- __float__(self)
- `self' must be ex(numeric).
- __getitem__(self, index)
- __init__(self, *args)
- ex() - return numeric('0')
ex(obj) - if n is ...
... Python number or string, return `number(obj)'.
... sequence, return `lst(obj)'. ex() function is applied to
all items of the sequence `obj'.
ex(ex(...)) -> ex(...)
- __int__(self)
- `self' must be ex(numeric).
- __len__(self)
- For ex(matrix), return number of rows.
For ex(numeric|symbol|constant), raise NotImplementedError.
For other ex objects, return nops().
- __lshift__(self, other)
- ex(pseries)<<int(other) - Return a new pseries object with the powers
shifted by degree `other'.
- __mul__(self, other)
- __neg__(self)
- __nonzero__(self)
- `self' must be ex(relational)|ex(numeric)
- __pos__(self)
- __pow__(self, other, m=None)
- __repr__(self)
- Return reproducible string representation of an ex object.
- __setitem__(self, index, other)
- __str__(self)
- Return pretty-print string representation of an ex object.
- __sub__(self, other)
- charpoly(self, sym)
- Characteristic Polynomial.
Following mathematica notation the characteristic polynomial of a
matrix M is defined as the determiant of `(M - sym * 1)' where 1
stands for the unit matrix of the same dimension as M. Note that
some CASs define it with a sign inside the determinant which gives
rise to an overall sign if the dimension is odd. This method
returns the characteristic polynomial collected in powers of
`sym' as a new expression.
`sym' must be a ex(symbol)|string.
- coeff(self, sym, n=1)
- Return coefficient of degree `n' in expressin `sym'.
`sym' must be an ex object.
- collect(self, sym)
- Sort expression in terms of powers of `sym'.
- cols(self)
- Get number of columns.
- content(self, sym)
- Compute content part (= unit normal GCD of all coefficients) of a
multivariate polynomial in Z[x]. The product of unit part, content
part, and primitive part is the polynomial itself.
`sym' must be an ex(symbol) object.
- degree(self, sym)
- Return degree of highest power in expression `sym'.
`sym' must be an ex object.
- denom(self)
- Return denominator of an expression.
If the expression is not of the normal form "numerator/denominator",
it is first converted to this form and then the denominator is returned.
- determinant(self, algo='automatic')
- Determinant of a square matrix.
If all the elements of the matrix are elements of an integral
domain the determinant is also in that integral domain and the
result is expanded only. If one or more elements are from a
quotient field the determinant is usually also in that quotient
field and the result is normalized before it is returned. This
implies that the determinant of the symbolic 2x2 matrix
[[a/(a-b),1],[b/(a-b),1]] is returned as unity. (In this respect,
it behaves like MapleV and unlike Mathematica.)
Option algo:
'automatic' - let the system choose
'gauss' - Gauss elimiation
'divfree' - Division-free elimination
'laplace' - Laplace (or minor) elimination
'bareiss' - Bareiss fraction-free elimination
- diff(self, sym, nth=1)
- Compute partial derivative of an expression.
`sym' must be an ex(symbol) object.
- eval(self, level=0)
- Perform automatic non-interruptive symbolic evaluation on expression.
- evalf(self, level=0)
- Evaluate object numerically.
- expand(self, options=0)
- Expand expression.
options:
1 - expand trigonometric,
2 - expand indexed.
- get_name(self)
- Return the name of a symbol|function.
- get_point(self)
- Get the expansion point.
`self' must be ex(pseries).
- get_var(self)
- Get the expansion variable.
`self' must be ex(pseries).
- has(self, other)
- Check if object contains any occurrences of `other'.
`other' must be an ex object.
- imag(self)
- Return imaginary part.
`self' must be ex(numeric).
- integer_content(self)
- Compute the integer content (= GCD of all numeric coefficients) of
an expanded polynomial.
- inverse(self)
- Inverse of a square matrix.
- is_add(self)
- Check if object is add.
- is_cinteger(self)
- Check if complex integer.
- is_compatible_to(self, other)
- Check if object is compatible pseries to pseries `other'
(i.e., expansion variable and point are the same).
- is_constant(self)
- Check if object is constant.
- is_crational(self)
- Check if complex rational.
- is_equal(self, obj)
- Test for equality.
This is only a quick test, meaning objects should be in the same
domain. You might have to .expand(), .normal() objects first,
depending on the domain of your computation, to get a more reliable
answer.
- is_even(self)
- Check if even integer.
- is_function(self)
- Check if object is function.
- is_integer(self)
- Check if integer.
- is_lst(self)
- Check if object is lst.
- is_matrix(self)
- Check if object is matrix.
- is_mul(self)
- Check if object is ncmul.
- is_negative(self)
- Check if negative number.
- is_nonneg_integer(self)
- Check if nonnegative integer.
- is_numeric(self)
- Check if object is numeric.
- is_odd(self)
- Check if odd integer.
- is_pos_integer(self)
- Check if positive integer.
- is_positive(self)
- Check if positive number.
- is_power(self)
- Check if object is power.
- is_prime(self)
- Check if integer prime.
- is_pseries(self)
- Check if object is pseries.
- is_rational(self)
- Check if rational.
- is_real(self)
- Check if real.
- is_relational(self)
- Check if object is relational.
- is_symbol(self)
- Check if object is symbol.
- is_terminating(self)
- Check if object is terminating pseries (i.e., without Order term).
- is_zero(self)
- Test for zero.
- lcoeff(self, sym)
- Return `coeff(sym, degree(sym))'.
- ldegree(self, sym)
- Return degree of lowest power in expression `sym'.
`sym' must be an ex object.
- lhs(self)
- Left hand side of relational expression.
- nops(self)
- Return number of operands/members.
- normal(self, level=0)
- Normalization of rational functions.
This function converts an expression to its normal form
"numerator/denominator", where numerator and denominator are
(relatively prime) polynomials. Any subexpressions which are not
rational functions (like non-rational numbers, non-integer powers or
functions like sin(), cos() etc.) are replaced by temporary symbols
which are re-substituted by the (normalized) subexpressions before
normal() returns (this way, any expression can be treated as a
rational function). normal() is applied recursively to arguments of
functions etc.
level - maximum depth of recursion.
- numer(self)
- Return numerator of an expression.
If the expression is not of the normal form "numerator/denominator",
it is first converted to this form and then the numerator is returned.
- op(self, i)
- Return operand/member at position `i'.
- primpart(self, sym, cont=None)
- Compute primitive part (using the content part `cont' for efficiency
if given) of a multivariate polynomial in Z[x]. The product of unit
part, content part, and primitive part is the polynomial itself.
`sym' must be an ex(symbol) object.
- real(self)
- Return real part.
`self' must be ex(numeric).
- rhs(self)
- Right hand side of relational expression.
- rows(self)
- Get number of rows.
- series(self, rel, order, options=0)
- Compute the truncated series expansion of an expression.
Return an expression containing an object ex(pseries) to
represent the series. If the series does not terminate within the
given truncation order, the last term of the series will be an order
term.
rel - expansion relation, lhs holds variable and rhs holds point.
order - truncation order of series calculations.
options = 1 - suppress branchcut.
`rel' must be an ex(relational) object or a 2-sequence.
- set_name(self, s)
- Set the name of a symbol to `s'.
- solve(self, vars, rhs, algo='automatic')
- Solve a linear system consisting of a m x n matrix and
a m x p right hand side by applying an elimination scheme to the
augmented matrix.
vars - n x p matrix, all elements must be symbols
rhs - m x p matrix
Option algo:
automatic - let the system choose
gauss - Gauss elimiation
divfree - Division-free elimination
bareiss - Bareiss fraction-free elimination
Return n x p solution matrix.
- subs(self, *args)
- Substitute objects in an expression (syntactic substitution) and
return the result as a new expression (that is already evaluated).
subs(e) - `e' must be ex(relational)|ex(lst(relational,...)).
subs(ls,lr) - `ls',`lr' are ex(lst)|ex
- swap(self, other)
- Swap efficiently the contents of two expressions.
`other' must be an ex object.
- tcoeff(self, sym)
- Return `coeff(sym, ldegree(sym))'.
- to_poly(self, no_order=0)
- Convert the pseries object to an ordinary polynomial.
If no_order, then discard higher order terms.
`self' must be ex(pseries).
- to_rational(self, repl_lst)
- Rationalization of non-rational functions.
This function converts a general expression to a rational polynomial
by replacing all non-rational subexpressions (like non-rational
numbers, non-integer powers or functions like sin(), cos() etc.) to
temporary symbols. This makes it possible to use functions like
gcd() and divide() on non-rational functions by applying
to_rational() on the arguments, calling the desired function and
re-substituting the temporary symbols in the result. To make the
last step possible, all temporary symbols and their associated
expressions are collected in the list specified by the `repl_lst'
parameter in the form {symbol == expression}, ready to be passed as
an argument to ex.subs().
`repl_lst' must be an ex(lst) object. It collects a GiNaC list of all
temporary symbols and their replacements.
- trace(self)
- Trace of a square matrix.
The result is normalized if it is in some quotient field and
expanded only otherwise. This implies that the trace of the
symbolic 2x2 matrix [[a/(a-b),x],[y,b/(b-a)]] is recognized to be
unity.
Return the sum of diagonal elements.
- transpose(self)
- Return transposed matrix.
- unit(self, sym)
- Compute unit part (= sign of leading coefficient) of a multivariate
polynomial in Z[x]. The product of unit part, content part, and
primitive part is the polynomial itself.
`sym' must be an ex(symbol) object.
|
class test_01_ex |
| |
Ex
--
>>> from ginac import ex,symbol,numeric
To create ex object from Python objects:
>>> ex()
numeric('0')
>>> ex(symbol('a'))
symbol('a')
>>> ex(3)
numeric('3')
>>> ex('3/4')
numeric('3/4')
>>> ex([])
lst([])
>>> ex([2,'5/4',symbol('g'),ex()])
lst([numeric('2'), numeric('5/4'), symbol('g'), numeric('0')])
>>> ex([[]])
lst([lst([])])
>>> ex([1,[symbol('c')]])
lst([numeric('1'), lst([symbol('c')])])
>>> ex({})
Traceback (most recent call last):
...
TypeError: ex_from_ref() argument must be number|string|ex|sequence (got <type 'dictionary'>)
Swap expressions:
>>> from ginac import swap
>>> a,b = symbol('a'),numeric('2/3')
>>> a.swap(b)
>>> a
numeric('2/3')
>>> b
symbol('a')
>>> a.swap(2)
Traceback (most recent call last):
...
TypeError: int
>>> swap(a,b)
>>> a,b
(symbol('a'), numeric('2/3'))
>>> swap(2,a)
Traceback (most recent call last):
...
TypeError: int
Operations with expressions:
>>> +b
numeric('2/3')
>>> -b
numeric('-2/3')
>>> a+b
add([symbol('a'), numeric('2/3')])
>>> a+1
add([symbol('a'), numeric('1')])
>>> b+2
numeric('8/3')
>>> 2+b
numeric('8/3')
>>> a+a
mul([symbol('a'), numeric('2')])
>>> a-a
numeric('0')
>>> a*a
power(symbol('a'), numeric('2'))
>>> a**5
power(symbol('a'), numeric('5'))
Expanding expressions:
>>> from ginac import expand
>>> e=(a+b)**2
>>> e
power(add([symbol('a'), numeric('2/3')]), numeric('2'))
>>> e.expand()
add([power(symbol('a'), numeric('2')), mul([symbol('a'), numeric('4/3')]), numeric('4/9')])
>>> e.expand(1)
add([power(symbol('a'), numeric('2')), mul([symbol('a'), numeric('4/3')]), numeric('4/9')])
>>> expand(e)
add([power(symbol('a'), numeric('2')), mul([symbol('a'), numeric('4/3')]), numeric('4/9')])
Pretty-printing:
>>> print a+b
2/3 + a
>>> print a*b
2/3 * a
>>> print a**b
a ** (2/3)
>>> print a-b
-2/3 + a
>>> print b-a
2/3 - a
>>> print -a
-a
>>> print (1+a) / a
(1 + a) / a
Accesing operants/members in ex objects:
>>> from ginac import nops,op
>>> e
power(add([symbol('a'), numeric('2/3')]), numeric('2'))
>>> e.nops(), nops(e)
(2, 2)
>>> e.op(0), op(e,0)
(add([symbol('a'), numeric('2/3')]), add([symbol('a'), numeric('2/3')]))
>>> e.op(1), op(e,1)
(numeric('2'), numeric('2'))
>>> e.op(-1)
Traceback (most recent call last):
...
IndexError: ex.op(-1) index out of range ([0...1])
>>> e.op(2)
Traceback (most recent call last):
...
IndexError: ex.op(2) index out of range ([0...1])
>>> a.nops()
0
>>> a.op(0)
Traceback (most recent call last):
...
IndexError: ex.op(0) index out of range ([0...-1])
Information about operants/members:
>>> from ginac import has
>>> e.has(a), has(e,a)
(1, 1)
>>> e.has(symbol()), has(e,symbol())
(0, 0)
>>> has(e,e)
1
Polynomial stuff:
>>> from ginac import degree,ldegree,coeff,lcoeff,tcoeff
>>> x,y=symbol('x'),symbol('y')
>>> p = 2*x**2 + x*y
>>> print p
y * x + 2 * x ** 2
>>> p.ldegree(x),p.degree(x)
(1, 2)
>>> p.ldegree(y),p.degree(y)
(0, 1)
>>> ldegree(p,x),degree(p,x)
(1, 2)
>>> print p.coeff(x,0)
0
>>> print p.coeff(x,1)
y
>>> print p.coeff(x,2)
2
>>> print p.coeff(x,10)
0
>>> print p.lcoeff(x)
2
>>> print p.tcoeff(x)
y
>>> print lcoeff(p, x), tcoeff(p, x)
2 y
>>> print coeff(p,x), coeff(p,y)
y x
>>> from ginac import numer,denom
>>> print p/x**2
(y * x + 2 * x ** 2) / x ** 2
>>> print numer(p/x**2), denom(p/x**2)
y + 2 * x x
>>> print (p/x**2).numer(), (p/x**2).denom()
y + 2 * x x
>>> r = -2*p/3+x
>>> print r.unit(x)
-1
>>> print r.content(x)
1
>>> print r.primpart(x)
2/3 * y * x + 4/3 * x ** 2 - x
>>> print r.primpart(x,1)
2/3 * y * x + 4/3 * x ** 2 - x
>>> print (30*r).integer_content()
10
Functions for rational functions:
>>> from ginac import normal
>>> print normal(x+1/x)
(1 + x ** 2) / x
>>> print normal(x+1/x,2)
x ** (-1) + x
>>> from ginac import lst,sin,to_rational
>>> l = lst()
>>> e = x+1/sin(x+1)
>>> print e.to_rational(l)
symbol8 ** (-1) + x
>>> l
lst([relational(symbol('symbol8'), sin(add([symbol('x'), numeric('1')])))])
>>> print l
[symbol8 == sin(1 + x)]
>>> l = lst()
>>> print to_rational(sin(x+1)+1/sin(x+1),l)
symbol9 + symbol9 ** (-1)
>>> print l
[symbol9 == sin(1 + x)]
>>> from ginac import collect
>>> print collect(2*x-x*sin(x),x)
x * (2 - sin(x))
>>> print (2*x-x*sin(x)).collect(x)
x * (2 - sin(x))
Evaluation of expressions:
>>> from ginac import eval,evalf
>>> e = sin(2*x/3)/3
>>> print e
1/3 * sin(2/3 * x)
>>> print e.evalf()
0.33333333333333333334 * sin(0.6666666666666666667 * x)
>>> print evalf(e)
0.33333333333333333334 * sin(0.6666666666666666667 * x)
>>> print e.evalf(2)
0.33333333333333333334 * sin(2/3 * x)
>>> print evalf(e,2)
0.33333333333333333334 * sin(2/3 * x)
Differentiation:
>>> from ginac import diff
>>> e = (sin(2*x)+x**2)/2
>>> print e.diff(x)
cos(2 * x) + x
>>> print diff(e,x)
cos(2 * x) + x
>>> print diff(e,x,3)
-4 * cos(2 * x)
>>> diff(e,1+x)
Traceback (most recent call last):
...
TypeError: symbol from_python() argument must be (string|ex(symbol)) (got ex(add))
Power series:
>>> from ginac import series
>>> print sin(x).series((x,0),5)
1 * x + (-1/6) * x ** 3 + Order(x ** 5)
>>> print series(sin(x)/x,(x,0),4)
1 + (-1/6) * x ** 2 + Order(x ** 3)
Substitution:
>>> from ginac import subs
>>> print sin(x).subs(x,x**2)
sin(x ** 2)
>>> print subs(sin(x),x,x**2)
sin(x ** 2)
>>> y = symbol('y')
>>> print subs(sin(x+y),[x,y],[x**2,x*y])
sin(y * x + x ** 2)
Auxiliary functions:
>>> from ginac import lhs,rhs
>>> rhs(x)
Traceback (most recent call last):
...
TypeError: ex.rhs() can be used only for ex(relational).
>>> lhs(x)
Traceback (most recent call last):
...
TypeError: ex.lhs() can be used only for ex(relational).
>>> x.rhs()
Traceback (most recent call last):
...
TypeError: ex.rhs() can be used only for ex(relational).
>>> (1+x).is_equal(x+1)
1
>>> (1+x).is_equal(x-1)
0
>>> (1+x).is_equal({})
0
>>> from ginac import is_zero
>>> is_zero(x), x.is_zero()
(0, 0)
>>> is_zero(x-x), (x-x).is_zero()
(1, 1)
>>> is_zero(0),is_zero(0.0),is_zero(34),is_zero({})
(1, 1, 0, 0)
>>> print numer(3), denom(3)
3 1
>>> print numer(3.3), denom(3.3)
3.2999999999999998224 1 |
| |
|
class test_02_symbol |
| |
Symbol
------
>>> from ginac import symbol,ex,is_symbol
To create a dummy symbol (it will have a name of the form `symbol\d+'):
>>> print repr(symbol())[:14]
symbol('symbol
To create a named symbol:
>>> symbol('name')
symbol('name')
Note that symbol applied to a symbol does nothing:
>>> symbol(symbol('aa'))
symbol('aa')
But when applied to a any other object than string or symbol,
an exception is raised:
>>> symbol(2)
Traceback (most recent call last):
...
TypeError: symbol from_python() argument must be (string|ex(symbol)) (got <type 'int'>)
Use .is_symbol() method to check whether an object is symbol or not:
>>> a = symbol()
>>> b = ex()
>>> a.is_symbol(),b.is_symbol()
(1, 0)
There is also a function for that:
>>> is_symbol(a), is_symbol([])
(1, 0)
To get the name of a symbol:
>>> a = symbol('abc')
>>> a.get_name()
'abc'
To set a new name to an existing symbol:
>>> a.set_name('foo')
>>> a
symbol('foo')
>>> a.get_name()
'foo' |
| |
|
class test_03_numeric |
| |
Numeric
-------
>>> from ginac import numeric,symbol,is_numeric,I
To construct numeric from Python objects:
>>> numeric(3)
numeric('3')
>>> numeric(1.5)
numeric('1.5')
>>> numeric(1+2j)
numeric('1.0+2.0*I')
>>> numeric('2/6')
numeric('1/3')
>>> numeric(numeric('2/5'))
numeric('2/5')
>>> numeric(2,7)
numeric('2/7')
>>> numeric()
numeric('0')
>>> 2*I+3
numeric('3+2*I')
But it fails for other objects like lists, etc.:
>>> numeric([2])
Traceback (most recent call last):
...
TypeError: numeric from_python() argument must be (number|string|ex(numeric)) (got <type 'list'>)
To check whether an expression is numeric:
>>> a,b=symbol(),numeric(2,3)
>>> a.is_numeric(),b.is_numeric()
(0, 1)
>>> is_numeric(b), is_numeric([])
(1, 0)
Auxiliary functions (and methods):
>>> from ginac import is_positive, is_integer, is_pos_integer,is_nonneg_integer
>>> map(is_positive,[1,-1,numeric(2),ex(-1),symbol(),[]])
[1, 0, 1, 0, 0, 0]
>>> from ginac import is_even, is_odd, is_prime, is_rational, is_real
>>> from ginac import is_cinteger, is_crational,is_negative
>>> from ginac import real,imag
>>> print int(numeric(23)),float(numeric(2.3))
23 2.3
Functions:
>>> from ginac import doublefactorial, bernoulli, fibonacci, isqrt,mod
>>> from ginac import gcd,lcm,irem,iquo
Precision:
>>> from ginac import set_Digits,get_Digits |
| |
|
class test_04_lst |
| |
Lst
------
Create GiNaC list from Python objects:
>>> from ginac import lst,is_lst, numeric
>>> lst()
lst([])
>>> lst([])
lst([])
>>> lst(2)
lst([numeric('2')])
>>> lst([2,[]])
lst([numeric('2'), lst([])])
>>> lst(lst([2,3]))
lst([numeric('2'), numeric('3')])
Check if object is lst:
>>> is_lst(lst()), is_lst([])
(1, 0)
>>> lst().is_lst(), numeric().is_lst()
(1, 0)
Slicing:
>>> a = lst(range(6))
>>> print a[1]
1
>>> print a[1:4]
[1, 2, 3]
>>> print a[5:1:-1]
[5, 4, 3, 2]
Modifying in-place:
>>> a[1] = 11
>>> a[3:] = [33,44,55]
>>> print a
[0, 11, 2, 33, 44, 55] |
| |
|
class test_05_add |
| |
Add
---
>>> from ginac import add,symbol,numeric,ex,is_add,is_numeric
>>> c = add(symbol('a'),numeric(3,4))
>>> c
add([symbol('a'), numeric('3/4')])
>>> c.is_add(), ex().is_add()
(1, 0)
>>> d = add(3,4)
>>> d
numeric('7')
>>> is_numeric(d), is_add(d), is_add({})
(1, 0, 0)
>>> add([])
numeric('0')
>>> add(3)
numeric('3')
>>> add([symbol('a'), numeric('3/4')])
add([symbol('a'), numeric('3/4')])
>>> add()
numeric('0')
>>> add({})
Traceback (most recent call last):
...
TypeError: ex_from_ref() argument must be number|string|ex|sequence (got <type 'dictionary'>) |
| |
|
class test_06_mul |
| |
Mul
---
>>> from ginac import mul,symbol,numeric,ex,is_mul,is_numeric
>>> c = mul(symbol('a'),numeric(3,4))
>>> c
mul([symbol('a'), numeric('3/4')])
>>> c.is_mul(), ex().is_mul()
(1, 0)
>>> d = mul(3,4)
>>> d
numeric('12')
>>> is_numeric(d), is_mul(d), is_mul({})
(1, 0, 0)
>>> mul([])
numeric('1')
>>> mul(3)
numeric('3')
>>> mul([symbol('a'), numeric('3/4')])
mul([symbol('a'), numeric('3/4')])
>>> mul()
numeric('1')
>>> mul({})
Traceback (most recent call last):
...
TypeError: ex_from_ref() argument must be number|string|ex|sequence (got <type 'dictionary'>) |
| |
|
class test_07_power |
| |
Power
------
>>> from ginac import power,symbol,is_power
>>> power(2,3)
numeric('8')
>>> power(symbol('a'),2)
power(symbol('a'), numeric('2'))
>>> power(symbol('a'),0)
numeric('1')
>>> p = power(symbol('a'),symbol('b'))
>>> p.is_power(), symbol().is_power()
(1, 0)
>>> is_power(p), is_power(1)
(1, 0)
>>> from ginac import sqrt
>>> print sqrt(-1)
I
>>> print sqrt(symbol('aa'))
sqrt(aa) |
| |
|
class test_08_inifcns |
| |
Functions
---------
>>> from ginac import abs,csgn,sin,cos,tan,exp,log,asin,acos,atan,atan2
>>> from ginac import sinh,cosh,tanh,asinh,acosh,atanh
>>> from ginac import Li2,Li3,zeta,lgamma,tgamma,psi
>>> from ginac import factorial,binomial,Order,Derivative
>>> from ginac import lsolve,symbol,lst,subs
>>> s = [symbol('a'),symbol('b'),symbol('c')]
>>> eqns = [s[0]+2*s[1]+3,s[1]-3*s[2]-5]
>>> print lst(eqns)
[3 + 2 * b + a, -5 - 3 * c + b]
>>> print lsolve(eqns,s)
[a == -13 - 6 * c, b == 5 + 3 * c, c == c]
>>> print lsolve(eqns,s[:2])
[a == -13 - 6 * c, b == 5 + 3 * c]
>>> print lsolve(eqns,s[1:])
[b == -3/2 - 1/2 * a, c == -13/6 - 1/6 * a]
>>> sol = lsolve(eqns,s)
>>> print subs(eqns, sol)
[0, 0] |
| |
|
class test_09_function |
| |
Function
------
To create a GiNaC function, use build_function:
>>> from ginac import build_function
To make a function with zero parameters:
>>> foo = build_function('foo')
>>> foo()
foo()
>>> print foo()
foo()
To make a function with one parameter:
>>> foo = build_function('foo',nofargs=1)
>>> foo(3)
foo(numeric('3'))
>>> print foo(3)
foo(3)
etc. Maximum number of parameters is 12:
>>> bar = build_function('bar',nofargs=12)
>>> print bar(1,2,3,4,5,6,7,8,9,0,1,2)
bar(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2)
>>> bar2 = build_function('bar2',nofargs=13)
Traceback (most recent call last):
...
NotImplementedError: _build_function() first argument must be less than 12
An exception is raised, when function is used with the wrong
number of parameters:
>>> foo(3,4)
Traceback (most recent call last):
...
TypeError: function foo() takes exactly 1 argument (2 given)
For automatic evaluation use user-defined evaluation function:
>>> from ginac import numeric
>>> def gun_eval(x):
... if x.is_negative(): return 0
... return gun(x,hold=1)
>>> gun = build_function('gun',eval_f=gun_eval)
>>> print gun(-1), gun(3)
0 gun(3)
Important:
Note the use of the `hold' keyword which stops futher automatic
evaluation of the function. You **MUST** use it when no more
evaluation is desired. Otherwise the evaluation function is called
recursively until something nasty happens (recursion depth is
reached, out of memory error,...).
Similary, you can submit functions for numerical evaluation (evalf_f,
must take nofargs arguments), for derivative (derivative_f, must take
nofargs+1 arguments), and for series expansion (series_f, must take
nofargs+3 arguments). For example,
>>> def sun_derivative(x,i):
... # return derivative in respect to the i-th parameter.
... assert int(i)==0
... if x.is_negative(): return 0
... return x*sun(x)
>>> sun = build_function('sun', derivative_f=sun_derivative)
>>> print sun(3)
sun(3)
>>> from ginac import symbol
>>> z = symbol('z')
>>> print sun(z).diff(z)
z * sun(z)
>>> print sun(z).diff(z,2)
z ** 2 * sun(z) + sun(z)
Note that build_function is able to determine the number of parameters
automatically from the given functions eval_f, evalf_f, derivative_f, series_f.
But in some occasions it is impossible. Therefore, giving the nofargs explicitly
is always recommended.
Auxiliary functions:
>>> from ginac import is_function, sin
>>> is_function(sin(2))
1
>>> sin(2).get_name()
'sin' |
| |
|
class test_10_relational |
| |
Relational
------
>>> from ginac import relational
>>> print relational(2,3)
2 == 3
>>> print relational(2,3,'==')
2 == 3
>>> print relational(2,3,'!=')
2 != 3
>>> print relational(2,3,'<')
2 < 3
>>> print relational(2,3,'<=')
2 <= 3
>>> print relational(2,3,'>')
2 > 3
>>> print relational(2,3,'>=')
2 >= 3
>>> relational(2,3,'aaaa')
Traceback (most recent call last):
...
TypeError: relational::operators from_python() argument must be (string: == | != | < | <= | > | >=) (got <type 'string'>)
>>> relational(2,3,'==')
relational(numeric('2'), numeric('3'))
>>> relational(2,3,'!=')
relational(numeric('2'), numeric('3'), '!=')
>>> relational(2,3,'<')
relational(numeric('2'), numeric('3'), '<')
>>> relational(2,3,'<=')
relational(numeric('2'), numeric('3'), '<=')
>>> relational(2,3,'>')
relational(numeric('2'), numeric('3'), '>')
>>> relational(2,3,'>=')
relational(numeric('2'), numeric('3'), '>=')
>>> relational(relational(2,3))
relational(numeric('2'), numeric('3'))
>>> print relational((2,3))
2 == 3
>>> print relational((2,3,'>'))
2 > 3
>>> from ginac import rhs,lhs,symbol
>>> print lhs(relational ((2,3)))
2
>>> print rhs(relational ((2,3)))
3
>>> print relational ((2,3)).lhs()
2
>>> print relational ((2,3)).rhs()
3
>>> lhs(symbol())
Traceback (most recent call last):
...
TypeError: ex.lhs() can be used only for ex(relational).
>>> not relational(2,3)
1
>>> not relational(2,2)
0
>>> from ginac import is_relational,numeric
>>> is_relational(2), is_relational(relational(2,3))
(0, 1)
>>> numeric().is_relational(), relational(2,3).is_relational()
(0, 1)
>>> a = relational(2,3)
>>> print a
2 == 3
>>> a[0] = 4
>>> print a
4 == 3 |
| |
|
class test_12_constant |
| |
Constant
------
>>> from ginac import constant, is_constant,numeric
>>> a=constant('a')
>>> a.is_constant(),numeric(2).is_constant()
(1, 0)
>>> a
constant('a')
>>> print a
a
>>> b = constant('b',1.5)
>>> print b, b.evalf()
b 1.5
>>> def c_eval():
... return numeric(2)/3
>>> c = constant('c',c_eval)
>>> print c, c.evalf()
c 0.6666666666666666667
>>> from ginac import Pi, Euler, Catalan
>>> Pi
constant('Pi')
>>> print Pi
Pi
>>> print Pi.evalf(), Euler.evalf(), Catalan.evalf()
3.1415926535897932385 0.5772156649015328606 0.91596559417721901505 |
| |
|
class test_13_matrix |
| |
Matrix
------
>>> from ginac import matrix,is_matrix,numeric
Column vector:
>>> print matrix([1,2])
[[1],
[2]]
Row vector:
>>> print matrix([[1,2]])
[[1, 2]]
Matrix:
>>> a = matrix([[1,2],[3,4]])
>>> print a
[[1, 2],
[3, 4]]
>>> a
matrix([[numeric('1'), numeric('2')],
[numeric('3'), numeric('4')]])
Take a row:
>>> print a[1]
[[3, 4]]
Take a column:
>>> print a[:,1]
[[2],
[4]]
Take an element:
>>> print a[0,1]
2
Auxiliary functions:
>>> a.is_matrix(),is_matrix(a),numeric().is_matrix(),is_matrix(numeric())
(1, 1, 0, 0)
Matrix constructors:
>>> print matrix(2,3)
[[0, 0, 0],
[0, 0, 0]]
>>> print matrix(2,3,range(6))
[[0, 1, 2],
[3, 4, 5]]
>>> print matrix(2,3,[1,2,3])
[[1, 2, 3],
[0, 0, 0]]
Slicing:
>>> a = matrix(3,4,range(12))
>>> print a
[[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]]
>>> print a[1:]
[[4, 5, 6, 7],
[8, 9, 10, 11]]
>>> print a[:,1:]
[[1, 2, 3],
[5, 6, 7],
[9, 10, 11]]
>>> print a[0:2, 1:3]
[[1, 2],
[5, 6]]
>>> print a[:,::-1]
[[3, 2, 1, 0],
[7, 6, 5, 4],
[11, 10, 9, 8]]
>>> print a[:,::2]
[[0, 2],
[4, 6],
[8, 10]]
>>> print a[1,1:-1]
[[5, 6]]
Modifying in-place:
>>> a = matrix(3,3)
>>> a[1,1] = 8 # middle element
>>> a[-1] = [3,2,1] # last row
>>> a[:,-1] = [5,4,1] # last column
>>> print a
[[0, 0, 5],
[0, 8, 4],
[3, 2, 1]]
>>> a = matrix(4,4) # 4x4 matrix of zeros
>>> a[1:3,1:3] = [[1,2],[3,4]]
>>> print a
[[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]]
Functions:
>>> from ginac import rows, cols, transpose
>>> rows(a), cols(a), a.rows(), a.cols()
(4, 4, 4, 4)
>>> print transpose(a)
[[0, 0, 0, 0],
[0, 1, 3, 0],
[0, 2, 4, 0],
[0, 0, 0, 0]]
>>> print a.transpose()
[[0, 0, 0, 0],
[0, 1, 3, 0],
[0, 2, 4, 0],
[0, 0, 0, 0]]
>>> from ginac import determinant,trace,inverse,charpoly
>>> a = matrix(2,2,[1,2,3,4])
>>> print a
[[1, 2],
[3, 4]]
>>> print a.determinant(), a.determinant('gauss')
-2 -2
>>> print determinant(a), determinant(a,'divfree')
-2 -2
>>> print a.trace(), trace(a)
5 5
>>> print a.inverse()
[[-2, 1],
[3/2, -1/2]]
>>> print a*inverse(a)
[[1, 0],
[0, 1]]
>>> print charpoly(a,'lambda')
-2 - 5 * lambda + lambda ** 2
>>> from ginac import symbol
>>> print a.charpoly(symbol('lambda'))
-2 - 5 * lambda + lambda ** 2
>>> syms = [symbol('a1'),symbol('a2')]
>>> print a.solve(syms,[5,-6])
[[-16],
[21/2]]
>>> print a*a.solve(syms,[5,-6])
[[5],
[-6]]
>>> from ginac import diag_matrix
>>> print diag_matrix([1,2,3])
[[1, 0, 0],
[0, 2, 0],
[0, 0, 3]] |
| |
|
class test_14_pseries |
| |
Power Series
------
>>> from ginac import is_pseries,pseries
>>> from ginac import is_terminating, is_compatible_to
>>> from ginac import symbol,series
>>> x = symbol('x')
>>> e = x**5 + 2*x**4 + 3*x**3 + 4*x**2 + 5 * x + 6
>>> p1 = series(e,(x,0),3)
>>> print p1
6 + 5 * x + 4 * x ** 2 + Order(x ** 3)
>>> p2 = series(e,(x,1),3)
>>> print p2
21 + 35 * (-1 + x) + 35 * (-1 + x) ** 2 + Order((-1 + x) ** 3)
>>> from ginac import sin
>>> p = series(sin(x),(x,0),5)
>>> p.is_pseries(), is_pseries(p), x.is_pseries(), is_pseries({})
(1, 1, 0, 0)
>>> p.is_compatible_to(p1), is_compatible_to(p, p1)
(1, 1)
>>> print p
1 * x + (-1/6) * x ** 3 + Order(x ** 5)
>>> print p + p1
6 + 6 * x + 4 * x ** 2 + Order(x ** 3)
>>> print p1 - p
6 + 4 * x + 4 * x ** 2 + Order(x ** 3)
>>> print p * p1
6 * x + 5 * x ** 2 + 3 * x ** 3 + Order(x ** 4)
>>> from ginac import Order
>>> r = pseries((x,0),[(3,-1),(2,2),(Order(1),3)])
>>> print r
3 * x ** (-1) + 2 * x ** 2 + Order(x ** 3)
>>> print r * p
3 + (-1/2) * x ** 2 + 2 * x ** 3 + Order(x ** 4)
>>> from ginac import series_to_poly
>>> print p.to_poly()
-1/6 * x ** 3 + Order(x ** 5) + x
>>> print p.to_poly(1)
-1/6 * x ** 3 + x
>>> print series_to_poly(p)
-1/6 * x ** 3 + Order(x ** 5) + x
>>> print r << 5 # shift powers by degree = 5
3 * x ** 4 + 2 * x ** 7 + Order(x ** 8)
>>> p=series(x,(x,1),2)
>>> print p
1 + 1 * (-1 + x)
>>> print p*x
1 + 2 * (-1 + x) + 1 * (-1 + x) ** 2
>>> print p ** 4
1 + 4 * (-1 + x) + 6 * (-1 + x) ** 2 + 4 * (-1 + x) ** 3 + Order((-1 + x) ** 4)
>>> print pow(p,4,2)
1 + 4 * (-1 + x) + Order((-1 + x) ** 2) |
| |
|
class test_15_ncmul |
| |
Non-commutative multiplication
------------------------------
>>> from ginac import is_ncmul,ncmul |
| |
| |