从Python转换/转换为Octave或Matlab [英] converting/ translate from Python to Octave or Matlab

查看:139
本文介绍了从Python转换/转换为Octave或Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/produced/numpy.r_.html 我只是不明白最后的输出,我仍然对_r的含义感到困惑,如何用Octave编写它?...我阅读了链接中写的内容,但对我来说仍然不清楚.

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.

推荐答案

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

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

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

np.r_[....]具有索引语法,最终被转换为对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])

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

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.

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

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 coords_inv返回的值之一;可以是标量或数组. x[0] x的第一个值.因此,r_可能会生成由x及其后续值组成的一维数组.

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.

这篇关于从Python转换/转换为Octave或Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆