rickyhitman10
rickyhitman10

Reputation: 59

Translate code matlab to python numpy

I don't understand about matlab programming, I just understand coding in python...

here my matrix want to solve

Matlab Code:

 x = ones(3,1).
con = [0.505868045540458,0.523598775598299].
series = [1,2,3] 
for j = 1:2 
   #here stuck to translate Python code#
   x = [x cos(con(j)*series)' sin(con(j)*series)'];
end

Result :

1.0000    0.8748    0.4846    0.8660    0.5000
1.0000    0.5304    0.8478    0.5000    0.8660
1.0000    0.0532    0.9986   -0.0000    1.0000

Anyone help me please, how to solve this problem... Regards!

Upvotes: 2

Views: 223

Answers (2)

hpaulj
hpaulj

Reputation: 231375

My recreation in an Ipython session (having first tested your code in an Octave session):

In [649]: con = np.array([0.505868, 0.5235897])
In [650]: series = np.array([1,2,3])
In [651]: x = [np.ones((3,))]
In [652]: for j in range(2):
     ...:     x.extend([np.cos(con[j]*series), np.sin(con[j]*series)])
     ...:     
In [653]: x
Out[653]: 
[array([ 1.,  1.,  1.]),
 array([ 0.8747542 ,  0.53038982,  0.05316725]),
 array([ 0.48456691,  0.84775388,  0.99858562]),
 array([  8.66029942e-01,   5.00015719e-01,   2.72267949e-05]),
 array([ 0.49999214,  0.86601633,  1.        ])]
In [654]: np.array(x).T
Out[654]: 
array([[  1.00000000e+00,   8.74754200e-01,   4.84566909e-01,
          8.66029942e-01,   4.99992140e-01],
       [  1.00000000e+00,   5.30389821e-01,   8.47753878e-01,
          5.00015719e-01,   8.66016328e-01],
       [  1.00000000e+00,   5.31672464e-02,   9.98585622e-01,
          2.72267949e-05,   1.00000000e+00]])

In MATLAB the

x = [x cos(...) sin(...)]

is closer to

x = np.concatenate([x, cos(...), sin(...)], axis=?)

but in numpy list append (or in this case extend) is faster. I just had to initial x to the appropriate list.

==================

I can get the same values without the loop

In [663]: y = con[:,None]*series
In [664]: [np.cos(y), np.sin(y)]
Out[664]: 
[array([[  8.74754200e-01,   5.30389821e-01,   5.31672464e-02],
        [  8.66029942e-01,   5.00015719e-01,   2.72267949e-05]]),
 array([[ 0.48456691,  0.84775388,  0.99858562],
        [ 0.49999214,  0.86601633,  1.        ]])]

but it's a bit of a pain to rearrange them into the order produced by iteration, [1, cos, sin, cos, sin].

Upvotes: 1

Eric
Eric

Reputation: 97571

Here's a solution that entirely removes the loop which resizes the array, which for large len(con) should make this more efficient than the matlab solution , and by association hpaulj's direct translation - this is linear in len(con) rather than quadratic.

import numpy as np

# declare the arrays
con = np.array([0.505868045540458, 0.523598775598299])
series = np.array([1,2,3])

# use broadcasting to generate trig_arg[i,j] = series[i]*con[j]
trig_arg = series[:,np.newaxis] * con

# Add another dimension that is either cos or sin
trig_elems = np.stack([np.cos(trig_arg), np.sin(trig_arg)], axis=-1)

# flatten out the last two dimensions
all_but_ones = trig_elems.reshape(trig_elems.shape[:-2] + (-1,))

# and add the first column of ones
result = np.concatenate([
    np.ones(series.shape)[:,np.newaxis],
    all_but_ones
], axis=-1)

Looking at each step along the way:

# configure numpy output to make it easier to see what's happening
>>> np.set_printoptions(suppress=True, precision=4)
>>> trig_arg
array([[ 0.5059,  0.5236],
       [ 1.0117,  1.0472],
       [ 1.5176,  1.5708]])
>>> trig_elems
array([[[ 0.8748,  0.4846],
        [ 0.866 ,  0.5   ]],

       [[ 0.5304,  0.8478],
        [ 0.5   ,  0.866 ]],

       [[ 0.0532,  0.9986],
        [-0.    ,  1.    ]]])

>>> all_but_ones
array([[ 0.8748,  0.4846,  0.866 ,  0.5   ],
       [ 0.5304,  0.8478,  0.5   ,  0.866 ],
       [ 0.0532,  0.9986, -0.    ,  1.    ]])

>>> result
array([[ 1.    ,  0.8748,  0.4846,  0.866 ,  0.5   ],
       [ 1.    ,  0.5304,  0.8478,  0.5   ,  0.866 ],
       [ 1.    ,  0.0532,  0.9986, -0.    ,  1.    ]])

np.stack is relatively new, but can be emulated with np.concatenate and some np.newaxis slicing. Or you can just go into the numpy source code and copy the new implementation of stack into your project if you're stuck with an older version.

Upvotes: 0

Related Questions