Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

from __future__ import print_function, division 

 

from .matexpr import MatrixExpr 

from sympy import Basic, sympify 

 

 

class FunctionMatrix(MatrixExpr): 

    """ 

    Represents a Matrix using a function (Lambda) 

 

    This class is an alternative to SparseMatrix 

 

    >>> from sympy import FunctionMatrix, symbols, Lambda, MatMul, Matrix 

    >>> i, j = symbols('i,j') 

    >>> X = FunctionMatrix(3, 3, Lambda((i, j), i + j)) 

    >>> Matrix(X) 

    Matrix([ 

    [0, 1, 2], 

    [1, 2, 3], 

    [2, 3, 4]]) 

 

    >>> Y = FunctionMatrix(1000, 1000, Lambda((i, j), i + j)) 

 

    >>> isinstance(Y*Y, MatMul) # this is an expression object 

    True 

 

    >>> (Y**2)[10,10] # So this is evaluated lazily 

    342923500 

    """ 

    def __new__(cls, rows, cols, lamda): 

        rows, cols = sympify(rows), sympify(cols) 

        return Basic.__new__(cls, rows, cols, lamda) 

 

    @property 

    def shape(self): 

        return self.args[0:2] 

 

    @property 

    def lamda(self): 

        return self.args[2] 

 

    def _entry(self, i, j): 

        return self.lamda(i, j) 

 

    def _eval_trace(self): 

        from sympy.matrices.expressions.trace import Trace 

        return Trace._eval_rewrite_as_Sum(Trace(self)).doit()