def test_bcdfo_evalP_5(self): """ We check that in the very easy case where the fonction to interpolate is always equal to 1 on the interpolant set, then the model value on any x point will logically be 1 """ Y = np.array([[ 1, 2, 1, 3, 3, 1],[1, 2, 2, 1, 1.01, 3 ]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1 , 1,1, 1e15) model = ( QZ.dot( np.linalg.solve( RZ.T , np.array([[1], [1], [1], [1], [1], [1] ]) ) )).T for i in range(0,50): res = bcdfo_evalP_( model, np.array([[(random()-0.5)*100],[(random()-0.5)*100]]), xbase, scale, 1 ) self.assertAlmostEqual(float(res), 1.0, places=15)
def test_bcdfo_evalP_2(self): """ We check that the model on the interpolant set equals the given values of the initial function on the interpolant set, here we verify that the sixth point is interpolated """ Y = np.array([[ 1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 1.01, 3 ]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1, 1, 1e15 ) #we verify that the sixth point is interpolated model = ( QZ.dot( np.linalg.solve( RZ.T , np.array([[random(),random(),random(),random(),random(), 6 ]]).T ) )).T res = bcdfo_evalP_( model, np.array([[1],[3]]), xbase, scale, 1 ) self.assertAlmostEqual(float(res), 6, places=13) #NB : the random numbers forces us to reduce the precision
def test_bcdfo_evalP_4(self): """ We check that the model on the interpolant set equals the given values of the initial function on the interpolant set, here we verify that the first point is interpolated """ Y = np.array([[ 1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 1.01, 3 ]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1, 1, 1e15 ) #we verify that the first point is interpolated model = ( QZ.dot( np.linalg.solve( RZ.T , np.array([[6,0,0,0o3,0,0 ]]).T ) )).T res = bcdfo_evalP_( model, np.array([[1],[1]]), xbase, scale, 1 ) self.assertAlmostEqual(float(res), 6, places=15)
def test_bcdfo_computeLj_1(self): """ This test verify the basic properties of the lagrange polynomials : We verify here that for x being a point of the interpolant set, we have l_j(x) = 1 if the index j corresponds to the index of x in the interpolant set and 0 otherwise """ for k in range(0, 20): x1_coord = [(random() - 0.5) * 100 for p in range(0, 6)] x2_coord = [(random() - 0.5) * 100 for p in range(0, 6)] Y = array([x1_coord, x2_coord]) whichmodel = 0 QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, whichmodel, 1, 1, 1, 1e15) for i in range(0, 6): L = bcdfo_computeLj_(QZ, RZ, i, Y, whichmodel, scale, 1) for j in range(0, 6): value = bcdfo_evalP_(L, array([[x1_coord[j]], [x2_coord[j]]]), xbase, scale, 1) if i == j: self.assertTrue(allclose(value, 1., atol=1e-10)) else: self.assertTrue(allclose(value, 0., atol=1e-10))
def bcdfo_evalL_(QZ=None,RZ=None,Y_=None,choice_set=None,x=None,xbase=None,whichmodel=None,scale=None,shift_Y=None,*args,**kwargs): """ # # Computes the values of the Lagrange polynomials at x. # # INPUTS: # # QZ, RZ : the QR factors of the (possibly shifted) matrix Z containing # the polynomial expansion of the interpolation points # Y : current interpolation set Y # choice_set : the indices (of Y's columns) for which to calculate the # Lagrange polynomial values # x : the point at which the model must be evaluated # xbase : the current base point # whichmodel : kind of model/Lagrange polynomial to compute # scale : the current model scaling # shift_Y : 0 if no shift in interpolation points, 1 otherwise # # OUTPUT: # # values : the values of the Lagrange polynomials at x. # # PROGRAMMING: A. Troeltzsch, September 2010. # # DEPENDENCIES: bcdfo_evalP # # TEST: # Y = [ 0 1 0 2 1 0 ; 0 0 1 0 0.01 2 ]; # [ QZ, RZ, xbase, scale] = bcdfo_build_QR_of_Y( Y, 0, 1 ); # values = bcdfo_evalL( QZ, RZ, Y, [2:6], [-1;1], xbase, 0, scale, 1 ) # should give # values = # 0 97.0000 2.9900 1.0000 -100.0000 -0.4950 # # CONDITIONS OF USE: Use at your own risk! No guarantee of any kind given. # """ # varargin = cellarray(args) # nargin = 9-[QZ,RZ,Y,choice_set,x,xbase,whichmodel,scale,shift_Y].count(None)+len(args) #Copy only if necessary if (shift_Y): Y=copy(Y_) else: Y=Y_ choice_set=choice_set.reshape(-1) n,p1=size_(Y,nargout=2) I=eye_(p1) lc=length_(choice_set) q=((n + 1) * (n + 2)) / 2 values=zeros_(p1,1) if (whichmodel == 3 and p1 < q): # for underdetermined regression model use min l2-norm model whichmodel=2 if (whichmodel == 0): # evaluate (sub-basis) Lagrange polynomial (p1 = q) values[choice_set]=bcdfo_evalP_(I[choice_set,:].dot( (numpy.linalg.solve(RZ,QZ.T))),x,xbase,scale,shift_Y) elif (whichmodel == 1): if (p1 == n + 1 or p1 == q): # evaluate Minimum L2 norm Lagrange polynomial (p1 == n+1 or p1 == q) values[choice_set]=bcdfo_evalP_(I[choice_set,:] .dot( (QZ / RZ.T)),x,xbase,scale,shift_Y) else: # evaluate Minimum Frobenius norm Lagrange polynomial (p1 <= q) # # If shifting is active, the matrix of the interpoaltion points is first # shifted and scaled (to compute M correctly). if (shift_Y): xbase=copy(Y[:,0]) scaleY=0 for i in range(0,p1): Y[:,i]=Y[:,i] - xbase scaleY=max_(scaleY,norm_(Y[:,i])) Y=Y / scaleY M=bcdfo_evalZ_(Y,q).T MQ=M[:,n + 1:q] if (shift_Y): phi=bcdfo_evalZ_((x - xbase) * scale[1],q) else: phi=bcdfo_evalZ_(x,q) values[choice_set]=concatenate_([ I[choice_set,:], zeros_(lc,n + 1).dot(QZ .dot(numpy.linalg.solve(RZ.T,concatenate_([MQ.dot(phi[n + 1:q]),phi[0:n + 1]]))))],axis=1) elif (whichmodel == 2): # evaluate Minimum L2 norm Lagrange polynomial (p1 <= q) if (p1 < q): values[choice_set]=bcdfo_evalP_(I[choice_set,:].dot( (pinv_(RZ).dot(QZ.T))),x,xbase,scale,shift_Y) else: values[choice_set]=bcdfo_evalP_(I[choice_set,:].dot( (numpy.linalg.solve(RZ,QZ.T))),x,xbase,scale,shift_Y) elif (whichmodel == 3): # evaluate Regression Lagrange polynomial (p1 >= q) values[choice_set]=bcdfo_evalP_(I[choice_set,:].dot((QZ .dot(pinv_(RZ.T)))),x,xbase,scale,shift_Y) return values