objects are not aligned. Matrix and Array
This program gives me the message following it:
================Program==========
import numpy as np
from numpy import matrix
import math
def sinD(D): # given in degrees, convert to radians
return math.sin(math.radians(D))
def cosD(D):
return math.cos(math.radians(D))
r = math.sqrt(2*2+5*5)
print r
m1 = matrix([[2], [5]])
print "m1: ", m1
theta = 5.0 # degrees
#CW 2x2 clockwise matrix
rotCW = matrix([ [cosD(theta), sinD(theta)], [-sinD(theta), cosD(theta)] ])
print rotCW
m2 = rotCW*m1
print "m2: ", m2
print "aaaaaaaa: ", type(m1), type(m2)
m1=np.array(m1)
m2=np.array(m2)
print "zzzzzzzz: ", type(m1), type(m2)
print"dot, ..."
dotres = np.dot(m1,m2)
print "dotres", dotres
==============end==========
==========Output msgs========
5.38516480713
m1: [[2]
[5]]
[[ 0.9961947 0.08715574]
[-0.08715574 0.9961947 ]]
m2: [[ 2.42816811]
[ 4.806662 ]]
aaaaaaaa:
On Fri, Dec 18, 2009 at 10:20 PM, Wayne Watson wrote: This program gives me the message following it:
================Program==========
import numpy as np
from numpy import matrix
import math You don't want math. def sinD(D): # given in degrees, convert to radians
return math.sin(math.radians(D))
def cosD(D):
return math.cos(math.radians(D)) def sinD(D):
return np.sin(np.deg2rad(D)) r = math.sqrt(2*2+5*5) np.hypot(2, 5) print r
m1 = matrix([[2], [5]])
print "m1: ", m1 theta = 5.0 # degrees
#CW 2x2 clockwise matrix
rotCW = matrix([ [cosD(theta), sinD(theta)], [-sinD(theta), cosD(theta)] ]) print rotCW m2 = rotCW*m1
print "m2: ", m2
print "aaaaaaaa: ", type(m1), type(m2)
m1=np.array(m1)
m2=np.array(m2) print "zzzzzzzz: ", type(m1), type(m2) print"dot, ..."
dotres = np.dot(m1,m2) Try np.dot(m2, m1), m1 is a column matrix. print "dotres", dotres
==============end========== ==========Output msgs========
5.38516480713
m1: [[2]
[5]]
[[ 0.9961947 0.08715574]
[-0.08715574 0.9961947 ]]
m2: [[ 2.42816811]
[ 4.806662 ]]
aaaaaaaa: Traceback (most recent call last):
File "C:/Sponsor_Meteors/Sentinel_Development/Development_Sentuser+Utilities/Playground/junk.py",
line 30, in <module>
dotres = np.dot(m1,m2)
ValueError: objects are not aligned
================end msgs===========
Why the msg? The types look alike and each array/matrix contains two
elements.. Chuck
Is math automatic (built-in)? Same result with np.dot(m2, m1). Ah, this works. dotres = np.dot(m2.T, m1). It looks to me like the shapes are the same, so maybe dot() requires one as a column vector and one as a row. Thanks. Charles R Harris wrote:
On Fri, Dec 18, 2009 at 10:20 PM, Wayne Watson
mailto:sierra_mtnview@sbcglobal.net> wrote: This program gives me the message following it: ================Program========== import numpy as np from numpy import matrix import math
You don't want math.
def sinD(D): # given in degrees, convert to radians return math.sin(math.radians(D)) def cosD(D): return math.cos(math.radians(D))
def sinD(D): return np.sin(np.deg2rad(D))
r = math.sqrt(2*2+5*5)
np.hypot(2, 5)
print r m1 = matrix([[2], [5]]) print "m1: ", m1
theta = 5.0 # degrees #CW 2x2 clockwise matrix rotCW = matrix([ [cosD(theta), sinD(theta)], [-sinD(theta), cosD(theta)] ])
print rotCW
m2 = rotCW*m1 print "m2: ", m2 print "aaaaaaaa: ", type(m1), type(m2) m1=np.array(m1) m2=np.array(m2)
print "zzzzzzzz: ", type(m1), type(m2)
print"dot, ..." dotres = np.dot(m1,m2)
Try np.dot(m2, m1), m1 is a column matrix.
print "dotres", dotres ==============end==========
==========Output msgs======== 5.38516480713 m1: [[2] [5]] [[ 0.9961947 0.08715574] [-0.08715574 0.9961947 ]] m2: [[ 2.42816811] [ 4.806662 ]] aaaaaaaa:
zzzzzzzz: dot, ... Traceback (most recent call last): File "C:/Sponsor_Meteors/Sentinel_Development/Development_Sentuser+Utilities/Playground/junk.py", line 30, in <module> dotres = np.dot(m1,m2) ValueError: objects are not aligned ================end msgs=========== Why the msg? The types look alike and each array/matrix contains two elements..
Chuck
------------------------------------------------------------------------
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"... humans'innate skills with numbers isn't much
better than that of rats and dolphins."
-- Stanislas Dehaene, neurosurgeon
Web Page:
On Sat, Dec 19, 2009 at 6:43 AM, Charles R Harris wrote: On Fri, Dec 18, 2009 at 10:20 PM, Wayne Watson <
sierra_mtnview@sbcglobal.net> wrote: This program gives me the message following it:
================Program==========
import numpy as np
from numpy import matrix
import math You don't want math. Why do you say that? The builtins are MUCH faster than numpy for single
values:
In [1]: import math
In [2]: import numpy as np
In [3]: %timeit np.sin(1.57)
100000 loops, best of 3: 2.41 us per loop
In [4]: %timeit math.sin(1.57)
10000000 loops, best of 3: 165 ns per loop
In [6]: %timeit np.array([np.sin(1.57)])
100000 loops, best of 3: 11.5 us per loop
In [7]: %timeit np.array([math.sin(1.57)])
100000 loops, best of 3: 7.01 us per loop
On Sat, Dec 19, 2009 at 4:53 AM, Chris Colbert
On Sat, Dec 19, 2009 at 6:43 AM, Charles R Harris < charlesr.harris@gmail.com> wrote:
On Fri, Dec 18, 2009 at 10:20 PM, Wayne Watson < sierra_mtnview@sbcglobal.net> wrote:
This program gives me the message following it: ================Program========== import numpy as np from numpy import matrix import math
You don't want math.
Why do you say that? The builtins are MUCH faster than numpy for single values:
In [1]: import math
In [2]: import numpy as np
In [3]: %timeit np.sin(1.57) 100000 loops, best of 3: 2.41 us per loop
In [4]: %timeit math.sin(1.57) 10000000 loops, best of 3: 165 ns per loop
In [6]: %timeit np.array([np.sin(1.57)]) 100000 loops, best of 3: 11.5 us per loop
In [7]: %timeit np.array([math.sin(1.57)]) 100000 loops, best of 3: 7.01 us per loop
Fair point. I was thinking of the vector case down the road. Chuck
participants (3)
-
Charles R Harris
-
Chris Colbert
-
Wayne Watson