【问题标题】:converting/ translate from Python to Octave or Matlab从 Python 转换/翻译为 Octave 或 Matlab
【发布时间】:2017-04-30 17:09:31
【问题描述】:

我有一个 Python 代码,想用 Octave 重写它,但在转换过程中遇到了很多问题。我为其中一些人找到了解决方案,其中一些人仍然需要你的帮助。现在我将从这部分代码开始:

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我只是不明白最后一个输出,我仍然混淆这里的_r是什么意思,我怎么能用Octave写它?....我读了链接中写的内容,但对我来说仍然不清楚。

【问题讨论】:

    标签: matlab python-2.7 numpy octave


    【解决方案1】:
    return np.r_[x,xoie,xooe,x[0]], np.r_[y,yoie,yooe,y[0]]
    

    该函数返回 2 个值,两个数组均由 np.r_ 创建。

    np.r_[....] 具有索引语法,并最终被转换为对np.r_ 对象的函数调用。结果只是参数的串联:

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

    使用[] 表示法,它可以接受slice 之类的对象(6:8),尽管我在这里看不到任何对象。我必须研究其余代码以确定其他参数是标量(单个值)还是数组。

    我的 Octave 生锈了(尽管我可以尝试转换)。

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

    xoiecoords_inv返回的值之一;可以是标量或数组。 x[0]x 的第一个值。所以r_ 可能会产生一个由x 和后续值组成的一维数组。

    【讨论】:

    • 谢谢你 hpaulj ...我只是在上面回答你,但我的回答总是被@Bhargav Rao 删除:)。现在回到代码,我认为我们在 Octave 中不需要 np,但是这个输出应该是怎样的呢?我这样写 x = [x,xoie,xooe,x(1)];但这是不对的? Octave 或 matlab 中的其他问题我应该从值 0 还是 1 开始?
    • np。省略了任何等效功能。索引从 1 开始。
    • 好的,非常感谢,但直到现在我还没有得到我主要问题的确切答案...您能否建议我输出它的外观...没错:x = [x,xoie,xooe,x(1)];
    猜你喜欢
    • 2014-07-19
    • 2011-11-02
    • 2020-05-20
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2011-02-03
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多