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 for numpy matrix operations #470

Closed
alexfeld opened this issue Dec 3, 2016 · 3 comments · Fixed by #965
Closed

Support for numpy matrix operations #470

alexfeld opened this issue Dec 3, 2016 · 3 comments · Fixed by #965
Labels
numpy Numpy related bug/enhancement

Comments

@alexfeld
Copy link

alexfeld commented Dec 3, 2016

A few things regarding matrix operations that aren't working properly that would be useful.

  • support for np.dot e.g. A.dot(b) or b.dot(A). Currently, if A,b are arrays containing quantities, this works. However, if A,b are Quantity's, then this operation does not return the correct result. I believe this is related to numpy: matrix multiplication by left give wrong results #39 .
  • support for np.linalg.solve. Solving a system where A,b are instances of Quantity the method fails with a TypeError in line 1235 of quantity. I think this is related to Prevent crash when context is None #420. If A,b are arrays of Quantitys the method fails in linalg with a TypeError: no loop matching the specified signature was found for ufunc solve1
@amine-aboufirass
Copy link

amine-aboufirass commented Apr 6, 2019

Has this been implemented? The documentation states that Pint supports numpy array operations. A dot product is one of the most basic array operations. Otherwise I don't believe there is a point in supporting numpy....

We could implement a temporary solution like this until the more fundamental solutions are resolved:

def pint_dot(arr1, arr2):
    units_arr1 = arr1.units
    units_arr2 = arr2.units
    arr3 = np.dot(arr1.magnitude, arr2.magnitude)
    units_arr3 = arr1.units*arr2.units
    return arr3*units_arr3

a = np.array([1,2,3])*ureg.m
b = np.array([2,3,4])*ureg.m

pint_dot(a,b).to('cm**2')

>>> 200000.0 centimeter ** 2

@hgrecco hgrecco added the numpy Numpy related bug/enhancement label Dec 3, 2019
@hgrecco
Copy link
Owner

hgrecco commented Dec 3, 2019

Revisit after #905

@jthielen
Copy link
Contributor

np.dot has been implemented in #905, but np.linalg.solve has not. I suspect it may be as simple as adding

for func_str in ['solve']:
    implement_func('function', func_str, input_units=None, output_unit='div')

to numpy_func.py, but it may be complicated by the fact that np.linalg.solve is in a submodule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numpy Numpy related bug/enhancement
Projects
None yet
4 participants