1 """Provides various implementation specific constants (expression heads, etc).
4 __docformat__
= 'restructuredtext'
9 """ Base class to head constants.
11 Head constants are singletons.
14 def __new__(cls
, *args
):
15 key
= '%s%s' % (cls
.__name
__, args
)
16 obj
= cls
._cache
.get(key
)
18 obj
= object.__new
__(cls
)
24 def init(self
, *args
):
25 # derived class may set attributes here
28 def as_unique_head(self
):
29 # used by the pickler support to make HEAD instances unique
31 return self
._cache
[self
._key
]
33 self
._cache
[self
._key
] = self
36 def is_data_ok(self
, cls
, expr
):
39 # The following constants define both the order of operands
40 # as well as placing parenthesis for classes deriving from
50 # The following constants are used by Verbatim and
51 # CollectingField classes.
56 #DENSE_POLY = HEAD('DENSE_POLY')
60 MATRIX_DICT
= intern('MATRIX_DICT')
61 MATRIX_DICT_T
= intern('MATRIX_DICT_T')
62 MATRIX_DICT_A
= intern('MATRIX_DICT_A')
63 MATRIX_DICT_TA
= intern('MATRIX_DICT_TA')
64 MATRIX_DICT_D
= intern('MATRIX_DICT_D')
65 MATRIX_DICT_TD
= intern('MATRIX_DICT_TD')
68 """ Matrix head singleton class.
72 MATRIX(<rows>, <cols>, <storage>)
76 ``<rows>`` - number of matrix rows
77 ``<cols>`` - number of matrix columns
78 ``<strorage>`` - constant describing data storage properties:
79 MATRIX_DICT, MATRIX_DICT_T, MATRIX_DICT_A, MATRIX_DICT_TA,
80 MATRIX_DICT_D, MATRIX_DICT_DT
84 return '%s(%r, %r, %r)' % (self
.__class
__.__name
__, self
.rows
, self
.cols
, self
.storage
)
88 def to_lowlevel(self
, cls
, data
, pair
):
91 def init(self
, rows
, cols
, storage
):
94 self
.shape
= (rows
, cols
)
95 self
.storage
= storage
97 self
.is_transpose
= is_transpose
= storage
in [MATRIX_DICT_T
, MATRIX_DICT_TA
, MATRIX_DICT_TD
]
98 self
.is_array
= storage
in [MATRIX_DICT_A
, MATRIX_DICT_TA
]
99 self
.is_diagonal
= storage
in [MATRIX_DICT_D
, MATRIX_DICT_TD
]
101 if storage
==MATRIX_DICT
:
102 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT_T
)
103 self
.A
= type(self
)(rows
, cols
, MATRIX_DICT_A
)
105 self
.D
= type(self
)(rows
, cols
, MATRIX_DICT_D
)
106 elif storage
==MATRIX_DICT_T
:
107 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT
)
108 self
.A
= type(self
)(rows
, cols
, MATRIX_DICT_TA
)
110 self
.D
= type(self
)(rows
, cols
, MATRIX_DICT_TD
)
111 elif storage
==MATRIX_DICT_A
:
112 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT_TA
)
114 self
.M
= type(self
)(rows
, cols
, MATRIX_DICT
)
115 self
.D
= type(self
)(rows
, cols
, MATRIX_DICT_D
)
116 elif storage
==MATRIX_DICT_TA
:
117 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT_A
)
119 self
.M
= type(self
)(rows
, cols
, MATRIX_DICT_T
)
120 self
.D
= type(self
)(rows
, cols
, MATRIX_DICT_TD
)
121 elif storage
==MATRIX_DICT_D
:
122 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT_T
)
123 self
.A
= type(self
)(rows
, cols
, MATRIX_DICT_A
)
124 self
.M
= type(self
)(rows
, cols
, MATRIX_DICT
)
126 elif storage
==MATRIX_DICT_TD
:
127 self
.T
= type(self
)(cols
, rows
, MATRIX_DICT
)
128 self
.A
= type(self
)(rows
, cols
, MATRIX_DICT_TA
)
129 self
.M
= type(self
)(rows
, cols
, MATRIX_DICT_T
)
132 raise NotImplementedError(`storage`
) #pragma NO COVER
134 def totree(obj
, tab
=''):
135 from .core
import Expr
136 if isinstance(obj
, Expr
):
137 head
, data
= obj
.pair
138 s
= '%s%s head:%r\n' % (tab
, type(obj
).__name
__, head
)
139 s
+= '%s' % (totree(data
, tab
+' '))
141 elif isinstance(obj
, dict):
142 return '%s%s:\n%s' % (tab
, type(obj
).__name
__, '\n'.join([totree(item
, tab
+' ') for item
in obj
.iteritems()]))
143 elif isinstance(obj
, (list, tuple)):
144 return '%s%s:\n%s' % (tab
, type(obj
).__name
__, '\n'.join([totree(item
, tab
+' ') for item
in obj
]))
146 return '%s%s:%r' % (tab
, type(obj
).__name
__, obj
)
149 def test_operations(operands
, expected_results
, unary_operations
, binary_operations
):
150 from sympycore
import Expr
153 for line
in expected_results
.split('\n'):
155 if ':' not in line
: continue
156 expr
, result
= line
.split(':')
157 for e
in expr
.split(';'):
159 results
[e
] = [r
.strip() for r
in result
.split(';')]
161 if isinstance(operands
, tuple):
162 operands1
, operands2
= operands
164 operands1
= operands2
= operands
166 for op1
in operands1
:
167 if isinstance(op1
, Expr
):
168 for op
in unary_operations
:
169 expr
= '%s(%s)' % (op
, op1
)
172 result_obj
= eval('%s(op1)' % (op
), dict(op1
=op1
))
173 result
= str(result_obj
)
174 except Exception, msg
:
175 print expr
,'failed with %s' % (msg
)
178 if expr
not in results
:
179 print '%s:%s' % (expr
, result
)
182 assert result
in results
[expr
], `results
[expr
], result`
183 except AssertionError:
185 print 'op1: %s' % (op1
)
186 print totree(op1
, ' ')
187 print '%r result: %s' % (op
, result_obj
)
188 print totree(result_obj
, ' ')
190 for op2
in operands2
:
191 if not (isinstance(op1
, Expr
) or isinstance(op2
, Expr
)):
193 for op
in binary_operations
:
194 expr
= '(%s)%s(%s)' % (op1
, op
, op2
)
198 result_obj
= eval('(op1)%s(op2)' % op
, dict(op1
=op1
, op2
=op2
))
199 result
= str(result_obj
)
200 except Exception, msg
:
202 if not result
.startswith('unsupported'):
203 print expr
,`op1
,op
,op2`
,'failed with %s' % (msg
)
205 if expr
not in results
:
206 print '%s:%s' % (expr
, result
)
208 if result
.startswith('unsupported'):
209 assert 'unsupported' in results
[expr
], `results
[expr
], result
, op1
, op2
, expr`
212 assert result
in results
[expr
], `results
[expr
], result`
213 except AssertionError:
215 print 'op1: %s' % (op1
)
216 print totree(op1
, ' ')
217 print 'op2: %s' % (op2
)
218 print totree(op2
, ' ')
219 print '%r result: %s' % (op
, result_obj
)
220 print totree(result_obj
, ' ')