Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SemanticPointer operations with numpy scalars #250

Closed
arvoelke opened this issue Jun 10, 2020 · 0 comments
Closed

Support SemanticPointer operations with numpy scalars #250

arvoelke opened this issue Jun 10, 2020 · 0 comments
Assignees
Milestone

Comments

@arvoelke
Copy link
Contributor

The expression:

spa.SemanticPointer(np.zeros(1)) * np.float64(1)

produces the error:

~/git/spa/nengo_spa/semantic_pointer.py in __mul__(self, other)
    227         If multiplied by a scalar, we do normal multiplication.
    228         """
--> 229         return self._mul(other, swap=False)
    230 
    231     def __rmul__(self, other):

~/git/spa/nengo_spa/semantic_pointer.py in _mul(self, other, swap)
    239         if is_array(other):
    240             raise TypeError(
--> 241                 "Multiplication of Semantic Pointers with arrays in not " "allowed."
    242             )
    243         elif is_number(other):

TypeError: Multiplication of Semantic Pointers with arrays in not allowed.

This is relevant because functions such as np.mean, np.sum, and np.linalg.norm, can all return numpy scalar types. For example,

sp = spa.SemanticPointer(np.zeros(1))
sp *= 1 / np.linalg.norm(sp.v)

produces the same error (note I am using multiplication instead of division because of #249, and yes I am aware of sp.normalized() -- this is just an example).

As a work-around, one can use .item() to convert the scalar to a native Python type, like so:

spa.SemanticPointer(np.zeros(1)) * np.float64(1).item()
@jgosmann jgosmann added this to the 1.0.2 milestone Jun 10, 2020
@jgosmann jgosmann added the bug label Jun 10, 2020
@jgosmann jgosmann self-assigned this Jun 22, 2020
jgosmann added a commit that referenced this issue Jun 22, 2020
for multiplication/division with SemanticPointer.

Fixes #250.
jgosmann added a commit that referenced this issue Jun 22, 2020
jgosmann added a commit that referenced this issue Jun 22, 2020
for multiplication/division with SemanticPointer.

This commit fixes #250.
jgosmann added a commit that referenced this issue Jun 22, 2020
jgosmann added a commit that referenced this issue Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants