File:  [CENS] / python / pyGiNaC / test4 / test_matrix.py
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Wed Nov 28 05:58:04 2001 UTC (15 years, 11 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Some cleanup

#!/usr/bin/env python
"""
  test_matrix - run ginac.matrix tests.
  Usage:
    python test_matrix.py [-v]
"""
"""
  This file is part of the PyGiNaC package.
  http://cens.ioc.ee/projects/pyginac/

  $Revision: 1.3 $
  $Id: test_matrix.py,v 1.3 2001-11-28 05:58:04 pearu Exp $
 
  Copyright 2001 Pearu Peterson all rights reserved,
  Pearu Peterson <pearu@cens.ioc.ee>
  Permission to use, modify, and distribute this software is given under the
  terms of the LGPL.  See http://www.fsf.org
 
  NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
"""

import test_all
import ginac

class test_matrix(ginac.matrix):
    """
    >>> a=symbol ('a')
    >>> b=symbol ('b')
    >>> c=symbol ('c')
    >>> p=diag_matrix ({0:[1,2,3]})
    >>> q=diag_matrix ({1:[5,6],-1:[0,0]})
    >>> test_save (a=a,b=b,c=c,m=matrix ([[2,a],[b,c]]),v=matrix ([a,b]),p=p,q=q)
    """
    def __init__(self):
        """
        >>> matrix([1,2])
        matrix([[numeric('1')],[numeric('2')]])
        >>> matrix ([[1,2]])
        matrix([[numeric('1'),numeric('2')]])
        """
    def __nonzero__ (self):
        """
        >>> not m
        0
        """
    def __repr__ (self):
        """
        >>> m
        matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]])
        >>> v
        matrix([[symbol('a')],[symbol('b')]])
        """
    def __str__ (self):
        """
        >>> print m
        [[2,a],[b,c]]
        >>> print v
        [[a],[b]]
        """
    def get_rows (self):
        """
        >>> m.get_rows ()
        2
        >>> v.get_rows ()
        2
        """
    def get_cols (self):
        """
        >>> m.get_cols ()
        2
        >>> v.get_cols ()
        1
        """
    def eval (self):
        """
        >>> m.eval ()
        matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]])
        """
    def evalf (self):
        """
        >>> m.evalf ()
        matrix([[numeric('2.0'),symbol('a')],[symbol('b'),symbol('c')]])
        """
    def evalm (self):
        """
        >>> m.evalm ()
        matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]])
        """
    def extract_archived (self):
        """
        >>> m.extract_archived ()
        {'row': [2L], 'class': 'matrix', 'col': [2L], 'm': [{'number': '2', 'class': 'numeric'}, {'class': 'symbol', 'name': 'a'}, {'class': 'symbol', 'name': 'b'}, {'class': 'symbol', 'name': 'c'}]}
        """
    def get_class_name (self):
        """
        >>> m.get_class_name ()
        'matrix'
        """
    def get_precedence (self):
        """
        >>> m.get_precedence ()
        70
        """
    def get_hash (self):
        """
        >>> type (m.get_hash ()) is type (0)
        1
        """
    def has (self):
        """
        >>> m.has (m)
        1
        >>> m.has (2)
        1
        >>> m.has (3)
        0
        >>> m.has (a)
        1
        >>> m.has (symbol ('d'))
        0
        """
    def is_equal (self):
        """
        >>> m.is_equal (m)
        1
        >>> m.is_equal (a)
        0
        >>> m.is_equal (1)
        0
        """
    def is_zero (self):
        """
        >>> m.is_zero ()
        0
        >>> matrix ([0]).is_zero ()
        0
        """
    def is_function (self):
        """
        >>> m.is_function ()
        0
        """
    def nops (self):
        """
        >>> m.nops ()
        4
        """
    def op (self):
        """
        >>> m.op (0)
        numeric('2')
        >>> m.op (1)
        symbol('a')
        >>> m.op (2)
        symbol('b')
        >>> m.op (3)
        symbol('c')
        >>> m.op (4)
        Traceback (most recent call last):
        ...
        IndexError: op(): index out of range
        """
    def to_list (self):
        """
        >>> m.to_list ()
        [numeric('2'), symbol('a'), symbol('b'), symbol('c')]
        """
    def to_context (self):
        """
        >>> m.to_context ()
        '[[2,a],[b,c]]'
        >>> m.to_context ('context')
        '[[2,a],[b,c]]'
        >>> m.to_context ('latex')
        '[[2,a],[b,c]]'
        >>> m.to_context ('csrc')
        '[[2.000000e+00,a],[b,c]]'
        """
    def swap (self):
        """
        >>> m.swap (v)
        >>> m,v
        (matrix([[symbol('a')],[symbol('b')]]), matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]]))
        >>> m.swap (v)
        >>> m,v
        (matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]]), matrix([[symbol('a')],[symbol('b')]]))
        """

    def transpose (self):
        """
        >>> m.transpose ()
        matrix([[numeric('2'),symbol('b')],[symbol('a'),symbol('c')]])
        >>> v.transpose ()
        matrix([[symbol('a'),symbol('b')]])
        """
    def trace (self):
        """
        >>> m.trace ()
        add(symbol('c'),numeric('2'))
        >>> v.trace ()
        Traceback (most recent call last):
        ...
        RuntimeError: matrix::trace(): matrix not square
        """
    def charpoly (self):
        """
        >>> m.charpoly (symbol ('lambda')).is_equal (2*c+(-2-c)*symbol ('lambda')-b*a+symbol ('lambda')**2)
        1
        >>> m.charpoly (a).is_equal (2*c+a**2+(-2-c-b)*a)
        1
        >>> v.charpoly (a)
        Traceback (most recent call last):
        ...
        RuntimeError: matrix::charpoly(): matrix not square
        """
    def inverse (self):
        """
        >>> m.inverse ().is_equal (matrix ([[c*(2*c-b*a)**(-1),-(2*c-b*a)**(-1)*a],[-b*(2*c-b*a)**(-1),2*(2*c-b*a)**(-1)]]).normal())
        1
        >>> v.inverse ()
        Traceback (most recent call last):
        ...
        RuntimeError: matrix::inverse(): matrix not square
        """
    def determinant (self):
        """
        >>> d = m.determinant()
        >>> d.is_equal(add(mul(symbol('b'),symbol('a'),numeric('-1')),mul(symbol('c'),numeric('2'))))
        1
        >>> m.determinant('gauss').is_equal(d)
        1
        >>> m.determinant('divfree').is_equal(d)
        1
        >>> m.determinant('bareiss').is_equal(d)
        1
        >>> m.determinant('laplace').is_equal(d)
        1
        >>> m.determinant('blaa')
        Traceback (most recent call last):
        ...
        ValueError: determinant_algo must be automatic|gauss|divfree|laplace|bareiss but got blaa
        """
    def solve (self):
        """
        >>> x,y=symbol('x'),symbol ('y')
        >>> xy = matrix ([x,y])
        >>> s=m.solve(diag_matrix({0:[1,1]}))
        >>> s.is_equal(matrix([[c*(2*c-b*a)**(-1),-(2*c-b*a)**(-1)*a],[-b*(2*c-b*a)**(-1),2*(2*c-b*a)**(-1)]]).normal())
        1
        >>> s=v.transpose().solve([1],[x,y])
        >>> s.is_equal (matrix([[mul(add(mul(symbol('b'),symbol('y'),numeric('-1')),numeric('1')),power(symbol('a'),numeric('-1')))],[symbol('y')]]).normal ())
        1
        """
    def __getitem__ (self):
        """
        >>> m[0]
        matrix([[numeric('2')],[symbol('b')]])
        >>> m[1]
        matrix([[symbol('a')],[symbol('c')]])
        >>> m[:1]
        matrix([[numeric('2')],[symbol('b')]])
        >>> m[::-1]
        matrix([[symbol('a'),numeric('2')],[symbol('c'),symbol('b')]])
        >>> m[:]
        matrix([[numeric('2'),symbol('a')],[symbol('b'),symbol('c')]])
        >>> z = diag_matrix({0:[1,2,3,4]})
        >>> z[1:3]
        matrix([[numeric('0'),numeric('0')],[numeric('2'),numeric('0')],[numeric('0'),numeric('3')],[numeric('0'),numeric('0')]])
        >>> z[::2]
        matrix([[numeric('1'),numeric('0')],[numeric('0'),numeric('0')],[numeric('0'),numeric('3')],[numeric('0'),numeric('0')]])
        >>> print z[::-1]
        [[0,0,0,1],[0,0,2,0],[0,3,0,0],[4,0,0,0]]
        >>> z[1,1]
        numeric('2')
        >>> z = diag_matrix({0:[1,2,3,4],2:[5,6]})
        >>> print z
        [[1,0,5,0],[0,2,0,6],[0,0,3,0],[0,0,0,4]]
        >>> print z[1,:]
        [[0,2,0,6]]
        >>> print z[:2,1:3]
        [[0,5],[2,0]]
        >>> print z[:,::-1]
        [[0,5,0,1],[6,0,2,0],[0,3,0,0],[4,0,0,0]]
        >>> print z[::-1,:]
        [[0,0,0,4],[0,0,3,0],[0,2,0,6],[1,0,5,0]]
        >>> print z[::-1,::-1]
        [[4,0,0,0],[0,3,0,0],[6,0,2,0],[0,5,0,1]]
        >>> print z[2][0,:]
        [[5]]
        >>> print z[2][0,0]
        5
        """
    def __setitem__ (self):
        """
        >>> z = diag_matrix({0:[1,2,3,4],2:[5,6]})
        >>> z[1]=10
        >>> print z
        [[1,10,5,0],[0,10,0,6],[0,10,3,0],[0,10,0,4]]
        >>> z[1,:] = 14
        >>> print z
        [[1,10,5,0],[14,14,14,14],[0,10,3,0],[0,10,0,4]]
        >>> z[1:3,1:3] = [-1,-2,-3,-4]
        >>> print z
        [[1,10,5,0],[14,-1,-2,14],[0,-3,-4,0],[0,10,0,4]]
        >>> z[1,2] = 4567
        >>> print z
        [[1,10,5,0],[14,-1,4567,14],[0,-3,-4,0],[0,10,0,4]]
        """
    def __add__ (self,other):
        """
        >>> print p+q
        [[0,5,0],[0,0,6],[0,0,0]]+[[1,0,0],[0,2,0],[0,0,3]]
        >>> print (p+q).evalm ()
        [[1,5,0],[0,2,6],[0,0,3]]
        """
    def __mul__ (self):
        """
        >>> print (p*q).evalm ()
        [[0,5,0],[0,0,12],[0,0,0]]
        """
    def __pow__ (self):
        """
        >>> print (p**-1).evalm ()
        [[1,0,0],[0,1/2,0],[0,0,1/3]]
        """
def _test():
    import test_matrix
    return test_all.test(test_matrix)

if __name__ == "__main__":
    _test()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>