converting/ translate from Python to Octave or Matlab

走远了吗. 提交于 2019-12-12 03:53:32

问题


I have a Python-Code and want to rewrite it in Octave, but I meet so many problems during the converting. I found a solution for some of them and some of them still need your help. Now i would start with this part of the code :

INVOLUTE_FI = 0
INVOLUTE_FO = 1
INVOLUTE_OI = 2
INVOLUTE_OO = 3
 def coords_inv(phi, geo,  theta, inv):
        """
        Coordinates of the involutes

        Parameters
        ----------
        phi : float
            The involute angle
        geo : struct
            The structure with the geometry obtained from get_geo()
        theta : float
            The crank angle, between 0 and 2*pi
        inv : int
            The key for the involute to be considered
        """

        rb = geo.rb
        ro = rb*(pi - geo.phi_fi0 + geo.phi_oo0)
        Theta = geo.phi_fie - theta - pi/2.0

        if inv == INVOLUTE_FI:
            x = rb*cos(phi)+rb*(phi-geo.phi_fi0)*sin(phi)
            y = rb*sin(phi)-rb*(phi-geo.phi_fi0)*cos(phi)
        elif inv == INVOLUTE_FO:
            x = rb*cos(phi)+rb*(phi-geo.phi_fo0)*sin(phi)
            y = rb*sin(phi)-rb*(phi-geo.phi_fo0)*cos(phi)
        elif inv == INVOLUTE_OI:
            x = -rb*cos(phi)-rb*(phi-geo.phi_oi0)*sin(phi)+ro*cos(Theta)
            y = -rb*sin(phi)+rb*(phi-geo.phi_oi0)*cos(phi)+ro*sin(Theta)
        elif inv == INVOLUTE_OO:
            x = -rb*cos(phi)-rb*(phi-geo.phi_oo0)*sin(phi)+ro*cos(Theta)
            y = -rb*sin(phi)+rb*(phi-geo.phi_oo0)*cos(phi)+ro*sin(Theta)
        else:
            raise ValueError('flag not valid')

        return x,y
    def CVcoords(CVkey, geo, theta, N = 1000):
        """ 
        Return a tuple of numpy arrays for x,y coordinates for the lines which 
        determine the boundary of the control volume
    Parameters
        ----------
        CVkey : string
            The key for the control volume for which the polygon is desired
        geo : struct
            The structure with the geometry obtained from get_geo()
        theta : float
            The crank angle, between 0 and 2*pi
        N : int
            How many elements to include in each entry in the polygon

        Returns
        -------
        x : numpy array
            X-coordinates of the outline of the control volume
        y : numpy array 
            Y-coordinates of the outline of the control volume
        """

        Nc1 = Nc(theta, geo, 1)
        Nc2 = Nc(theta, geo, 2)

        if CVkey == 'sa':

            r = (2*pi*geo.rb-geo.t)/2.0

            xee,yee = coords_inv(geo.phi_fie,geo,0.0,'fi')
            xse,yse = coords_inv(geo.phi_foe-2*pi,geo,0.0,'fo')
            xoie,yoie = coords_inv(geo.phi_oie,geo,theta,'oi')
            xooe,yooe = coords_inv(geo.phi_ooe,geo,theta,'oo')
            x0,y0 = (xee+xse)/2,(yee+yse)/2 

            beta = atan2(yee-y0,xee-x0)
            t = np.linspace(beta,beta+pi,1000)
            x,y = x0+r*np.cos(t),y0+r*np.sin(t)
              return np.r_[x,xoie,xooe,x[0]],np.r_[y,yoie,yooe,y[0]]

https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html I just don´t understand the last Output, and I am still confuse what´s mean _r here, and how can I write it by Octave?....I read what is written in the link, but it still not clear for me.


回答1:


return np.r_[x,xoie,xooe,x[0]], np.r_[y,yoie,yooe,y[0]]

The function returns 2 values, both arrays created by np.r_.

np.r_[....] has indexing syntax, and ends up being translated into a function call to the np.r_ object. The result is just the concatenation of the arguments:

In [355]: np.r_[1, 3, 6:8, np.array([3,2,1])]
Out[355]: array([1, 3, 6, 7, 3, 2, 1])

With the [] notation it can accept slice like objects (6:8) though I don't see any of those here. I'd have to study the rest of the code to identify whether the other arguments are scalars (single values) or arrays.

My Octave is rusty (though I could experiment with the conversion).

t = np.lispace... # I think that exists in Octave, a 1000 values
x = x0+r*np.cos(t)  # a derived array of 1000 values

xoie one of the values returned by coords_inv; may be scalar or array. x[0] the first value of x. So the r_ probably produces a 1d array made up of x, and the subsequent values.



来源:https://stackoverflow.com/questions/41158385/converting-translate-from-python-to-octave-or-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!