Ask Your Question

Manuels's profile - activity

2023-09-06 10:06:16 +0200 received badge  Favorite Question (source)
2016-09-23 03:40:58 +0200 received badge  Great Question (source)
2012-09-24 11:27:39 +0200 received badge  Famous Question (source)
2011-12-22 16:16:30 +0200 received badge  Notable Question (source)
2011-08-03 11:26:06 +0200 received badge  Popular Question (source)
2011-04-13 06:25:29 +0200 received badge  Supporter (source)
2011-04-13 06:25:28 +0200 marked best answer Symbolic matrices

There is the Tensor module in sympy. If that is the type of thing that you're looking for (ie explicit indices).

The was some code put on the sage devel group last year: abstract matrices, which allows basic manipulation of abstract matrices and vectors. It's not complete, but it does the basics and should be easy to extend if you need more. As far as I can tell, it hasn't been worked on since the original postings. I hope it's ok if I post the code here:

from sage.structure.element import Element 
from sage.combinat.free_module import CombinatorialFreeModule 

# TODO: doc and tests for all of those 
class SymbolicMatrix(SageObject): 
    def __init__(self, name, nrows, ncols, inverted = False, transposed = False): 
        #Element.__init__(self, parent) 
        self._name = name 
        self._nrows = nrows 
        self._ncols = ncols 
        self._inverted = inverted 
        self._transposed = transposed 

    def _repr_(self): 
        result = self._name 
        if self._inverted: 
            result += "^-1" 
        if self._transposed: 
            result += "^t" 
        return result 

    def transpose(self): 
        result = copy(self) 
        result._transposed = not self._transposed 
        (result._nrows, result._ncols) = (self._ncols, self._nrows) 
        return result 

    def __invert__(self): 
        assert self._nrows == self._ncols, "Can't inverse non square matrix" 
        result = copy(self) 
        result._inverted = not self._inverted 
        return result 


class SymbolicMatrixAlgebra(CombinatorialFreeModule): 
    r""" 

    EXAMPLES:: 

        sage: Alg = SymbolicMatrixAlgebra(QQ) 

        sage: A = Alg.matrix("A",3,2) 
        sage: B = Alg.matrix("B",2,3) 
        sage: C = Alg.matrix("C",2,3) 
        sage: D = Alg.matrix("D",3,3) 

    Example 1: Adding/Multiplying matrices of correct size:: 

        sage: A * (B + C) 
        A B + A C 

    Example 2: Transposing a sum of matrices:: 

        sage: (B + C).transpose() 
        C^t + B^t 

    Example 3: Transposing a product of matrices:: 

        sage: (A * B).transpose() 
        B^t A^t 

    Example 4: Inverting a product of matrices:: 

        sage: (A * B)^-1 
        B^-1 A^-1 

    Example 5: Multiplying by its inverse:: 

        sage: D * D^-1 # todo: not implemented 
        I 

    TODO: decide on the best output; do we want to be able to 
    copy-paste back? do we prefer short notations? 

    .. warnings:: 

    The identity does not know it is size, so the following should 
    complain, but does not:: 

        sage: D * D^-1 * A 

    TODO: describe all the abuses 
    """ 

    def __init__(self, R): 
        """ 
        EXAMPLES:: 

            sage: A = AlgebrasWithBasis(QQ).example(); A 
                An example of an algebra with basis: the free algebra on the generators ('a', 'b', 'c') over Rational Field 
            sage: TestSuite(A).run() 

        """ 
        CombinatorialFreeModule.__init__(self, R, Words(), category = AlgebrasWithBasis(R)) 

    def matrix(self, name, nrows, ncols): 
        """ TODO: doctest""" 
        return self.monomial(Word([SymbolicMatrix(name, nrows, ncols)])) 

    def _repr_(self): 
        """ 
        EXAMPLES:: 

            sage: SymbolicMatrixAlgebra(QQ) 
            The symbolic matrix algebra over Rational Field 
        """ 
        return "The symbolic matrix algebra over %s"%(self.base_ring()) 

    @cached_method 
    def one_basis(self): 
        """ 
        Returns the empty word, which index the one of this algebra, 
        as per :meth:`AlgebrasWithBasis.ParentMethods.one_basis`. 

        EXAMPLES:: 

            sage: Alg = SymbolicMatrixAlgebra(QQ) 
            sage: Alg.one_basis() 
            word: 
            sage: A.one() 
            I 
        """ 
        return self.basis().keys()([]) 

    def product_on_basis(self, w1, w2): 
        r""" 
        Product of basis elements, as per :meth:`AlgebrasWithBasis.ParentMethods.product_on_basis ...
(more)
2011-04-13 06:25:28 +0200 received badge  Scholar (source)
2011-04-12 19:43:40 +0200 received badge  Good Question (source)
2011-04-12 12:50:30 +0200 received badge  Nice Question (source)
2011-04-12 10:45:18 +0200 received badge  Student (source)
2011-04-12 08:01:26 +0200 asked a question Symbolic matrices

Hi,

is it possible to define totally symbolic matrices. Something like

N,M = var('N,M')
a = matrix(SR, N, M)
b = matrix(SR, M, N)
c = a.dot(b)

So that c[i,j] = sum(a[i,k]*b[k,i],k,1,M) or do you really always need an non-symblic matrix size?

If you really always need a non-symblic matrix type, is it possible to define the symbolic variables automatically? Something like

a = matrix(SR, 2, 3)
print a[i,j]
output:
a_(i,j)

Thanks!

Manuel