[docs]defsimplify(self,ratio=1.7,measure=count_ops):"""Applies simplify to the elements of a matrix in place. This is a shortcut for M.applyfunc(lambda x: simplify(x, ratio, measure)) See Also ======== sympy.simplify.simplify.simplify """foriinrange(len(self._mat)):self._mat[i]=_simplify(self._mat[i],ratio=ratio,measure=measure)

[docs]deffill(self,value):"""Fill the matrix with the scalar value. See Also ======== zeros ones """self._mat=[value]*len(self)

[docs]deflist2numpy(l):# pragma: no cover"""Converts python list of SymPy expressions to a NumPy array. See Also ======== matrix2numpy """fromnumpyimportemptya=empty(len(l),dtype=object)fori,sinenumerate(l):a[i]=sreturna

[docs]defmatrix2numpy(m):# pragma: no cover"""Converts SymPy's matrix to a NumPy array. See Also ======== list2numpy """fromnumpyimportemptya=empty(m.shape,dtype=object)foriinrange(m.rows):forjinrange(m.cols):a[i,j]=m[i,j]returna

@doctest_depends_on(modules=('numpy',))

[docs]defsymarray(prefix,shape):# pragma: no cover"""Create a numpy ndarray of symbols (as an object array). The created symbols are named ``prefix_i1_i2_``... You should thus provide a non-empty prefix if you want your symbols to be unique for different output arrays, as SymPy symbols with identical names are the same object. Parameters ---------- prefix : string A prefix prepended to the name of every symbol. shape : int or tuple Shape of the created array. If an int, the array is one-dimensional; for more than one dimension the shape must be a tuple. Examples -------- These doctests require numpy. >>> from sympy import symarray >>> symarray('', 3) [_0 _1 _2] If you want multiple symarrays to contain distinct symbols, you *must* provide unique prefixes: >>> a = symarray('', 3) >>> b = symarray('', 3) >>> a[0] == b[0] True >>> a = symarray('a', 3) >>> b = symarray('b', 3) >>> a[0] == b[0] False Creating symarrays with a prefix: >>> symarray('a', 3) [a_0 a_1 a_2] For more than one dimension, the shape must be given as a tuple: >>> symarray('a', (2, 3)) [[a_0_0 a_0_1 a_0_2] [a_1_0 a_1_1 a_1_2]] >>> symarray('a', (2, 3, 2)) [[[a_0_0_0 a_0_0_1] [a_0_1_0 a_0_1_1] [a_0_2_0 a_0_2_1]] <BLANKLINE> [[a_1_0_0 a_1_0_1] [a_1_1_0 a_1_1_1] [a_1_2_0 a_1_2_1]]] """fromnumpyimportempty,ndindexarr=empty(shape,dtype=object)forindexinndindex(shape):arr[index]=Symbol('%s_%s'%(prefix,'_'.join(map(str,index))))returnarr

[docs]defones(r,c=None):"""Returns a matrix of ones with ``r`` rows and ``c`` columns; if ``c`` is omitted a square matrix will be returned. See Also ======== zeros eye diag """from.denseimportMatrixifis_sequence(r):SymPyDeprecationWarning(feature="The syntax ones([%i, %i])"%tuple(r),useinstead="ones(%i, %i)."%tuple(r),issue=3381,deprecated_since_version="0.7.2",).warn()r,c=relse:c=rifcisNoneelsecr=as_int(r)c=as_int(c)returnMatrix(r,c,[S.One]*r*c)

[docs]defzeros(r,c=None,cls=None):"""Returns a matrix of zeros with ``r`` rows and ``c`` columns; if ``c`` is omitted a square matrix will be returned. See Also ======== ones eye diag """ifclsisNone:from.denseimportMatrixasclsreturncls.zeros(r,c)

[docs]defGramSchmidt(vlist,orthog=False):""" Apply the Gram-Schmidt process to a set of vectors. see: http://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process """out=[]m=len(vlist)foriinrange(m):tmp=vlist[i]forjinrange(i):tmp-=vlist[i].project(out[j])ifnottmp.values():raiseValueError("GramSchmidt: vector set not linearly independent")out.append(tmp)iforthog:foriinrange(len(out)):out[i]=out[i].normalized()returnout