Hi,
On 3 September 2013 13:38, Wojciech Czaja <[email protected]> wrote:
>
> When I create Matrix calling g (gclass object)
> I get KeyError but calling g directly I do not
>
> Any idea?
>
For some, yet unknown to me, reason Matrix constructor converts first
component to a SymPy object, so (1, 1) get converted to (Integer(1),
1). Your code works without issuing g(1, 1) before Matrix(4, 4, ...).
Try not to mix different types within one instance of gclass and
you'll be fine. This strange behavior seems a bug to me, but there
might be some reason to do this.
Example:
In [1]: %paste
class gclass(object):
def __init__(self):
self.components = {}
def __call__(self, *idxs):
if idxs in self.components.keys():
return self.components[idxs]
else:
component = sum(idxs[i] for i in range(len(idxs)))
self.components.update({idxs: component})
return component
## -- End pasted text --
In [2]: g = gclass()
In [3]: Matrix(4, 4, lambda i, j: g(i, j))
Out[3]:
⎡0 1 2 3⎤
⎢ ⎥
⎢1 2 3 4⎥
⎢ ⎥
⎢2 3 4 5⎥
⎢ ⎥
⎣3 4 5 6⎦
In [4]: g(1, 1)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-4-3733e998b6af> in <module>()
----> 1 g(1, 1)
<ipython-input-1-f4d7dee41d32> in __call__(self, *idxs)
4 def __call__(self, *idxs):
5 if idxs in self.components.keys():
----> 6 return self.components[idxs]
7 else:
8 component = sum(idxs[i] for i in range(len(idxs)))
KeyError: (1, 1)
In [5]: g.components
Out[5]:
{(0, 0): 0, (0, 1): 1, (0, 2): 2, (0, 3): 3, (1, 0): 1, (1, 1): 2, (1,
2): 3, (1, 3): 4, (2, 0): 2, (2, 1): 3, (2, 2): 4, (2, 3): 5, (3, 0)
: 3, (3, 1): 4, (3, 2): 5, (3, 3): 6}
In [6]: (1, 1) in g.components
Out[6]: False
In [7]: (Integer(1), 1) in g.components
Out[7]: True
btw. sum(idxs[i] for i in range(len(idxs))) can be simplified to
sum(idxs) because you can iterate over tuples in Python.
> class gclass(object):
> def __init__(self):
> self.components = {}
> def __call__(self, *idxs):
> if idxs in self.components.keys():
> return self.components[idxs]
> else:
> component = sum(idxs[i] for i in range(len(idxs)))
> self.components.update({idxs: component})
> return component
>
> g = gclass()
> g(1, 1)
> print g(1, 1)
> Matrix(4,4, lambda i,j: g(i, j))
>
> ---------------------------------------------------------------------------
> KeyError Traceback (most recent call last)
> <ipython-input-50-17a658aa0dab> in <module>()
> 13 g(1, 1)
> 14 print g(1, 1)
> ---> 15 Matrix(4,4, lambda i,j: g(i, j))
>
> /usr/local/lib/python2.7/dist-packages/sympy/matrices/dense.pyc in
> __new__(cls, *args, **kwargs)
> 606
> 607 def __new__(cls, *args, **kwargs):
> --> 608 return cls._new(*args, **kwargs)
> 609
> 610 def as_mutable(self):
>
> /usr/local/lib/python2.7/dist-packages/sympy/matrices/dense.pyc in _new(cls,
> *args, **kwargs)
> 598 @classmethod
> 599 def _new(cls, *args, **kwargs):
> --> 600 rows, cols, flat_list = cls._handle_creation_inputs(*args,
> **kwargs)
> 601 self = object.__new__(cls)
> 602 self.rows = rows
>
> /usr/local/lib/python2.7/dist-packages/sympy/matrices/matrices.pyc in
> _handle_creation_inputs(cls, *args, **kwargs)
> 146 for i in range(rows):
> 147
> flat_list.extend([cls._sympify(operation(cls._sympify(i), j))
> --> 148 for j in range(cols)])
> 149
> 150 # Matrix(2, 2, [1, 2, 3, 4])
>
> <ipython-input-50-17a658aa0dab> in <lambda>(i, j)
> 13 g(1, 1)
> 14 print g(1, 1)
> ---> 15 Matrix(4,4, lambda i,j: g(i, j))
>
> <ipython-input-50-17a658aa0dab> in __call__(self, *idxs)
> 4 def __call__(self, *idxs):
> 5 if idxs in self.components.keys():
> ----> 6 return self.components[idxs]
> 7 else:
> 8 component = sum(idxs[i] for i in range(len(idxs)))
>
> KeyError: (1, 1)
>
> 2
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
Mateusz
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.