Advertisement
Guest User

Linear Interpolation Python (From Interparc)

a guest
Dec 19th, 2013
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.34 KB | None | 0 0
  1. import numpy as np
  2. import scipy.interpolate as sp
  3. import math
  4. import csv
  5.  
  6. def diffCOL(matrix):
  7.     newMAT = []
  8.     newROW = []
  9.     for i in range(len(matrix)-1):
  10.         for j in range(len(matrix[i])):
  11.             diff = matrix[i+1][j]-matrix[i][j]
  12.             newROW.append(diff)
  13.         newMAT.append(newROW)
  14.         newROW = []
  15.     #Stack the matrix to get xyz in columns
  16.     newMAT = np.vstack(newMAT)
  17.     return newMAT
  18.    
  19. def squareELEM(matrix):            
  20.     for i in range(len(matrix)):
  21.         for j in range(len(matrix[i])):
  22.             matrix[i][j] = matrix[i][j]*matrix[i][j]
  23.     return matrix
  24.        
  25. def sumROW(matrix):      
  26.     newMAT = []
  27.     for j in range(len(matrix)):
  28.         rowSUM = 0
  29.         for k in range(len(matrix[j])):
  30.             rowSUM = rowSUM + matrix[j][k]
  31.         newMAT.append(rowSUM)  
  32.     return newMAT
  33.            
  34. def sqrtELEM(matrix):
  35.     for i in range(len(matrix)):
  36.         matrix[i] = math.sqrt(matrix[i])
  37.     return matrix      
  38.    
  39. def sumELEM(matrix):
  40.     sum = 0
  41.     for i in range(len(matrix)):
  42.         sum = sum + matrix[i]
  43.     return sum
  44.    
  45. def diffMAT(matrix,denom):
  46.     newMAT = []
  47.     for i in range(len(matrix)):
  48.         newMAT.append(matrix[i]/denom)
  49.     return newMAT
  50.    
  51. def cumsumMAT(matrix):
  52.     first = 0
  53.     newmat = []
  54.     newmat.append(first)
  55.     #newmat.append(matrix)
  56.     for i in range(len(matrix)):
  57.         newmat.append(matrix[i])
  58.     cum = 0
  59.     for i in range(len(newmat)):
  60.         cum = cum + newmat[i]
  61.         newmat[i] = cum
  62.     return newmat
  63.        
  64. def divMAT(A,B):
  65.     newMAT = []
  66.     for i in range(len(A)):
  67.         newMAT.append(A[i] / B[i])
  68.     return newMAT
  69.  
  70. def minusVector(t,cumarc):
  71.     newMAT = []
  72.     for i in range(len(t)):
  73.         newMAT.append(t[i] - cumarc[i])
  74.     return newMAT
  75.    
  76. def replaceIndex(A,B):
  77.     newMAT = []
  78.     print "A: ,",A
  79.     print "LENA: ",len(A)
  80.     print "B: ,",B
  81.     print "LENB: ",len(B)
  82.     for i in range(len(B)):
  83.         index = B[i]
  84.         newMAT.append(A[index])
  85.     return newMAT        
  86.    
  87. def matSUB(first,second):
  88.     newMAT = []
  89.     newCOL = []
  90.     for i in range(len(first)):
  91.         for j in range(len(first[i])):
  92.             newMAT.append(first[i][j] - second[i][j])
  93.         #newMAT.append(newCOL)
  94.     return newMAT
  95.    
  96. def matADD(first,second):
  97.     newMAT = []
  98.     newCOL = []
  99.     #print "F: ",first
  100.     #print "S: ",second
  101.     for i in range(len(first)):
  102.         for j in range(len(first[i])):
  103.             newMAT.append(first[i][j]+second[i][j])
  104.         #newMAT.append(newCOL)
  105.     return newMAT
  106.    
  107. def matMULTI(first,second):
  108.     """
  109.    Take in two matrix
  110.    multiply each element against the other at the same index
  111.    return a new matrix
  112.    """
  113.     #print "FIRST: ",first
  114.     #print "SECOND: ",second
  115.     newMAT = []
  116.     newCOL = []
  117.     for i in range(len(first)):
  118.         for j in range(len(first[i])):
  119.             newMAT.append(first[i][j]*second[i][j])
  120.         #newMAT.append(newCOL)
  121.     return newMAT
  122.    
  123. def matDIV(first,second):
  124.     """
  125.    Take in two matrix
  126.    multiply each element against the other at the same index
  127.    return a new matrix
  128.    """
  129.     newMAT = []
  130.     newCOL = []
  131.     for i in range(len(first)):
  132.         for j in range(len(first[i])):
  133.             newMAT.append(first[i][j]/second[i][j])
  134.         #newMAT.append(newCOL)
  135.     return newMAT
  136.  
  137. def vecDIV(first,second):
  138.     """
  139.    Take in two arrays
  140.    multiply each element against the other at the same index
  141.    return a new array
  142.    """
  143.     newMAT = []
  144.     for i in range(len(first)):
  145.         newMAT.append(first[i]/second[i])
  146.     return newMAT
  147.  
  148. def replaceROW(matrix,replacer,adder):
  149.     newMAT = []
  150.     #print "MAT: ",matrix
  151.     #print "REP: ",replacer
  152.     if adder != 0:
  153.         for i in range(len(replacer)):
  154.             newMAT.append(matrix[replacer[i]+adder])
  155.     else:
  156.         for i in range(len(replacer)):
  157.             newMAT.append(matrix[replacer[i]])
  158.     return np.vstack(newMAT)
  159.            
  160.            
  161. def interparc(t,px,py,*args):
  162.     inputs = [t,px,py,args]
  163.     #print "T: ",t
  164.     #print "X: ",px
  165.     #print "Y: ",py
  166.     #print "Z: ",varargin
  167.    
  168.     #If we dont get at least a t, x, and y value we error
  169.     if len(inputs)<3:
  170.         print "ERROR: NOT ENOUGH ARGUMENTS"
  171.        
  172.     #Should check to make sure t is a single integer greater than 1
  173.     t = t
  174.     if (t>1) and (t%1==0):
  175.         t = np.linspace(0,1,t)
  176.     elif t<0 or t>1:
  177.         print "Error: STEP SIZE t IS NOT ALLOWED"
  178.        
  179.     nt = len(t)
  180.    
  181.     px = px
  182.     py = py
  183.     n = len(px)
  184.    
  185.     if len(px) != len(py):
  186.         print "ERROR: MUST BE SAME LENGTH"
  187.     elif n < 2:
  188.         print "ERROR: MUST BE OF LENGTH 2"
  189.        
  190.     pxy = [px,py]
  191.     #pxy = np.transpose(pxy)
  192.     ndim = 2
  193.    
  194.     method = 'linear'
  195.    
  196.     if len(args) > 1:
  197.         if isinstance(args[len(args)-1], basestring) == True:
  198.             method = args[len(args)-1]
  199.             if method != 'linear' and method != 'pchip' and method != 'spline':
  200.                 print "ERROR: INVALID METHOD"
  201.     elif len(args)==1:
  202.         method = args[0]
  203.     print method
  204.     method = 'linear'
  205.     # Try to append all the arguments together
  206.     for i in range(len(args)):
  207.         if isinstance(args[i], basestring) != True:
  208.             pz = args[i]
  209.             print "Z",len(pz)
  210.             if len(pz) != n:
  211.                 print "ERROR: LENGTH MUST BE SAME AS OTHER INPUT"
  212.             pxy.append(pz)
  213.     print "DIM LEN: ",len(pxy)
  214.     ndim = len(pxy)
  215.    
  216.     pt = np.zeros((nt,ndim))
  217. #Check for rounding errors here
  218.     # Transpose the matrix to align with matlab codes method
  219.     pxy = np.transpose(pxy)
  220.     chordlen = sqrtELEM(sumROW(squareELEM(diffCOL(pxy))))
  221.     chordlen = diffMAT(chordlen,sumELEM(chordlen))
  222.     cumarc = cumsumMAT(chordlen)
  223.     print ""
  224.     print ""
  225.     print "Third operation (CUMARC): \n",np.vstack(cumarc)
  226.     print ""
  227.  
  228.     if method == 'linear':
  229.         inter = np.histogram(bins=t,a=cumarc)
  230.         tbins = inter[1]
  231.         hist= inter[0]
  232.         tbinset=[]
  233.         index=0
  234.         tbinset.append(index)
  235.         print "ORIGINAL TBIN: \n",hist
  236.         print "LEN: \n",len(hist)
  237.         print ""
  238.        
  239.         for i in range(len(hist)):
  240.             if hist[i]>0:
  241.                 index=index+hist[i]
  242.                 tbinset.append(index)
  243.             else:
  244.                 tbinset.append(index)
  245.  
  246.         for i in range(len(tbinset)):
  247.             if tbinset[i]<= 0 or t[i]<=0:
  248.                 tbinset[i] = 1
  249.             elif tbinset[i]>=n or t[i]>=1:
  250.                 tbinset[i] = n-1
  251.         print ""
  252.         print "TBINSET: ",tbinset
  253.         print "LEN: ",len(tbinset)
  254.         #Take off one value to match the way matlab does indexing
  255.         for i in range(len(tbinset)):
  256.             tbinset[i]=tbinset[i]-1
  257.  
  258.         s = divMAT(minusVector(t,replaceIndex(cumarc,tbinset)),replaceIndex(chordlen,tbinset) )
  259.  
  260.         #Breakup the parts of pt
  261.         repmat = np.transpose(np.reshape(np.vstack(np.tile(s,(1,ndim))[0]),(ndim,-1)))
  262.         sub = np.reshape( np.vstack( matSUB( replaceROW(pxy,tbinset,1) , replaceROW(pxy,tbinset,0) ) ) , (-1,ndim) )
  263.         multi = np.reshape( np.vstack(matMULTI( sub , repmat )) ,(-1,ndim) )
  264.         pt = np.reshape( np.vstack( matADD( replaceROW(pxy,tbinset,0) , multi ) ) ,(-1,ndim) )
  265.         return pt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement