Element wise operations on matrices

101 views
Skip to first unread message

Daniele Nicolodi

unread,
Oct 26, 2017, 6:10:52 PM10/26/17
to sympy
Hello,

I'm just getting started with SymPy, so this mnay be something obvious
or maybe I am doing it completely wrong. Any help is greatly appreciated.

I would like SymPy to compute for me some matrix algebra and return the
expressions for the manipulations of the matrix elements. One way I
found to do this is the following:

A = Matrix(MatrixSymbol('a', 1, 3))
B = Matrix(MatrixSymbol('b', 3, 3))
z = A * B

which returns the familiar looking matrix elements expression. However,
if I try to compute the inverse of matrix B, I get an error:

b.inv()

AttributeError Traceback (most recent call last)
<ipython-input-168-f0554a79bc7e> in <module>()
----> 1 b.inv()

//anaconda/lib/python3.5/site-packages/sympy/matrices/matrices.py in
inv(self, method, **kwargs)
307 if method is not None:
308 kwargs['method'] = method
--> 309 return self._eval_inverse(**kwargs)
310
311 def inv_mod(self, m):

//anaconda/lib/python3.5/site-packages/sympy/matrices/dense.py in
_eval_inverse(self, **kwargs)
304 M = self.as_mutable()
305 if method == "GE":
--> 306 rv = M.inverse_GE(iszerofunc=iszerofunc)
307 elif method == "LU":
308 rv = M.inverse_LU(iszerofunc=iszerofunc)

//anaconda/lib/python3.5/site-packages/sympy/matrices/matrices.py in
inverse_GE(self, iszerofunc)
2627
2628 big = Matrix.hstack(self.as_mutable(),
Matrix.eye(self.rows))
-> 2629 red = big.rref(iszerofunc=iszerofunc, simplify=True)[0]
2630 if any(iszerofunc(red[j, j]) for j in range(red.rows)):
2631 raise ValueError("Matrix det == 0; not invertible.")

//anaconda/lib/python3.5/site-packages/sympy/matrices/matrices.py in
rref(self, iszerofunc, simplify)
2685 break
2686 if simplify:
-> 2687 r[pivot, i] = simpfunc(r[pivot, i])
2688 if iszerofunc(r[pivot, i]):
2689 for k in xrange(pivot, r.rows):

//anaconda/lib/python3.5/site-packages/sympy/simplify/simplify.py in
simplify(expr, ratio, measure, fu)
3706 _e = cancel(expr)
3707 expr1 = shorter(_e, _mexpand(_e).cancel()) # issue 6829
-> 3708 expr2 = shorter(together(expr, deep=True), together(expr1,
deep=True))
3709
3710 if ratio is S.Infinity:

//anaconda/lib/python3.5/site-packages/sympy/simplify/simplify.py in
shorter(*choices)
3700 if not has_variety(choices):
3701 return choices[0]
-> 3702 return min(choices, key=measure)
3703
3704 expr = bottom_up(expr, lambda w: w.normal())

//anaconda/lib/python3.5/site-packages/sympy/core/function.py in
count_ops(expr, visual)
2250 while args:
2251 a = args.pop()
-> 2252 if a.is_Rational:
2253 #-1/3 = NEG + DIV
2254 if a is not S.One:

AttributeError: 'str' object has no attribute 'is_Rational'

which I don't get if I construct a Matrix object manually:

b = symbols('b_:3\,:3');
B = Matrix([[b[0], b[1], b[2]],
[b[3], b[4], b[5]],
[b[6], b[7], b[8]]])
simplify(B.inv())

but this gets very boring very quickly for large matrices.

Is this a bug or am I doing it the wrong way?

Thanks!

Cheers,
Daniele

Aaron Meurer

unread,
Oct 26, 2017, 9:07:31 PM10/26/17
to sy...@googlegroups.com
This is a bug. In master, I get a different error

Traceback (most recent call last):
File "<mypython-4>", line 1, in <module>
B.inv()
File "./sympy/matrices/matrices.py", line 2797, in inv
return self._eval_inverse(**kwargs)
File "./sympy/matrices/dense.py", line 260, in _eval_inverse
rv = M.inverse_GE(iszerofunc=iszerofunc)
File "./sympy/matrices/matrices.py", line 2710, in inverse_GE
red = big.rref(iszerofunc=iszerofunc, simplify=True)[0]
File "./sympy/matrices/matrices.py", line 889, in rref
normalize_last=normalize_last)
File "./sympy/matrices/matrices.py", line 544, in _eval_rref
zero_above=True)
File "./sympy/matrices/matrices.py", line 660, in _row_reduce
get_col(piv_col)[piv_row:], iszerofunc, simpfunc)
File "./sympy/matrices/matrices.py", line 4130, in _find_reasonable_pivot
if x.equals(S.Zero):
File "./sympy/core/expr.py", line 657, in equals
constant = diff.is_constant(simplify=False, failing_number=True)
File "./sympy/core/expr.py", line 606, in is_constant
deriv = expr.diff(w)
File "./sympy/core/expr.py", line 2967, in diff
return Derivative(self, *new_symbols, **assumptions)
File "./sympy/core/function.py", line 1137, in __new__
if v._diff_wrt:
AttributeError: 'MatrixSymbol' object has no attribute '_diff_wrt'

Also, ideally you would be able to just do MatrixSymbol('b', 3,
3).inv().as_explicit(), but apparently that doesn't work either.

I have opened https://github.com/sympy/sympy/issues/13541 for this.

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/c0ca19a8-4269-bac2-74dc-99283d3f5305%40grinta.net.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages