2013-08-20 3 views
-1

일부 matlab 코드를 파이썬으로 변환하려고 시도하고 matlab의 mkpp 기능을 사용했습니다. scipy를 사용하여 파이썬에서 보간법에 대해 여러 가지를 읽었지만 matlab 코드의 기능을 모방하는 방법을 이해할 수 없습니다. scipy에서 interpolate.PiecewisePolynomial을 사용해 보았습니다.하지만 matlab에서 break와 계수 인 같은 인수를 취하는 것 같습니다. MATLAB이 실제로하고있는 것의 근원이 있습니까? mkpp의 기능을 파이썬에서 어떻게 모방 할 수 있습니까?matlabs mkpp를 파이썬으로 변환

편집 : 그래서 아래의 코멘트에서, 그래 나는

if nargin==2, d = 1; else d = d(:).'; end 
dlk=numel(coefs); l=length(breaks)-1; dl=prod(d)*l; k=fix(dlk/dl+100*eps); 
if (k<=0)||(dl*k~=dlk) 
    error(message('MATLAB:mkpp:PPNumberMismatchCoeffs',... 
     int2str(l),int2str(d),int2str(dlk))) 
end 

pp.form = 'pp'; 
pp.breaks = reshape(breaks,1,l+1); 
pp.coefs = reshape(coefs,dl,k); 
pp.pieces = l; 
pp.order = k; 
pp.dim = d; 

파일의 말씀인가, 나는 PP에 대한 모든 파일을 찾을 수 없습니다, 그래서 쓸모없는 것 같다의 mkpp 파일을보고 노력했다.

Ive는 다음을 통해 읽습니다. Can someone explain the behavior of the functions mkpp and ppval? 그러나 Matlab이 장면 뒤에서하고있는 일은 여전히 ​​설명하지 않습니다. 단지 matlab이 일반 다항식과 다르다는 것을 설명합니다.

내가 알아 내려고 시작하는 문제는 scipy에서 찾은 모든 것이 mkpp와 다른 인수를 취한다는 것입니다. 나는 coefs를 만들고 나누기 위해 파이썬 코드를 작성했지만, scipy 문서 http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PiecewisePolynomial.html#scipy.interpolate.PiecewisePolynomial은 변수를 같은 방식으로 참조하지 않아서 매우 혼란 스럽습니다. 사용되는 용어에 관심있는 사람들을위한

+0

당신은 MATLAB 명령 창에 귀하의 MATLAB 디렉토리에'mkpp.m' 파일 또는 '편집 mkpp'에 관심이있을 수 있습니다. – marsei

+0

편집 mkpp doesnt 많이 표시 : 만약 nargin == 2, d = 1; else d = d (:). '; 끝 dlk = numel (coefs); l = 길이 (나누기) -1; dl = prod (d) * l; k = 수정 (dlk/dl + 100 * eps); 오류 (메시지 ('MATLAB : mkpp : PPNumberMismatchCoeffs', ... int2str (l), int2str (d), int2str (dlk)) 경우 (k <= 0)) 끝 pp.form = 'pp'; pp.breaks = reshape (중단, 1, l + 1); pp.coefs = reshape (coefs, dl, k); pp.pieces = 1; pp.order = k; pp.dim = d; – user1938107

답변

1

을 무엇을 의미하는지 누군가에 관해서는이 구문 분석하는 데 도움 수 있다면

그것은 좋은 것, 여기 내 혼란이었고, 이것이 내가 지금 그것을 이해하는 방법입니다 것입니다. pp는 실제로 함수가 아니라 다항식을 정의한 구조체에 대한 호출입니다. mkpp는 다항식의 구성 요소를 생성하는 것으로, ppval 또는 스플라인 또는 다른 분석 방법과 함께 사용할 수 있습니다. 내가 다항식

class PiecePoly(): 
    """ 
    A class to mimick the MATLAB struct piecewise polynomial (pp) 
    """ 
    def __init__(self): 
     form = 'pp' 
     breaks = [] 
     coefs = [] 
     pieces = 0 
     order = 0 
     dim = 0 
를 정의하는 클래스를 사용

먼저이를 모방하기 위해, 나는 다음과 같은 코드 진행중인 작업이지만이 어떻게 작동하는지에 관한 좋은 아이디어를 줄 것이다 작성

그런 다음 함수 mkpp은 다음과 같습니다

def mkpp(breaks,coefs,*args): 
    """ 
    Takes in the breaks, coefs, and optionally (d) then creates a pp from the 
    PiecePoly class and constructs the polynomial 
    Returns: the constructed polynomial 
    """ 
    if len(args)==1: 
     d = np.transpose(args[0]) 
    else: 
     d = 1 
    sum=0 
    try: 
     #Just make sure coefs is not a 4D matrix 
     for i in range(len(coefs)): 
      for j in range(len(coefs[i])): 
       sum = sum+len(coefs[i][j]) 
    except: 
     #First try to count a 2D coefs array this should be the one that works 
     try: 
      for i in range(len(coefs)): 
       sum = sum+len(coefs[i]) 
     #Coefs must be 1 dimensional 
     except: 
      sum = len(coefs) 

    dlk = sum 
    l = len(breaks)-1 

    try: 
     if len(d) > 1: 
      prod = 0 
      for i in range(len(d)): 
       prod = prod*d[i] 
      dl = prod*l 
     else: 
      dl = d*l 
    except: 
     dl = d*l 

    k = dlk/dl+100*(math.pow(2,-52)) 
    if k<0: 
     k = math.ceil(k) 
    else: 
     k = math.floor(k) 

    if k<=0 or (dl*k!=dlk): 
     print "ERROR: MISMATCH PP AND COEF" 
     return None 

    pp = PiecePoly() 
    pp.form = 'pp' 
    pp.breaks = np.reshape(breaks,1,l+1) 
    pp.coefs = np.reshape(coefs,dl,k) 
    pp.order = k 
    pp.dim = d 

    return pp