def test_bcdfo_poisedness_Y(self): """ This is the test written in the Matlab Code. Results are the same. """ Y = array([[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, 1, 1, 1e15) lSolver = 1 whichmodel = 0 hardcons = 0 stratLam = 1 lambd, Y_radius = bcdfo_poisedness_Y_(QZ, RZ, Y, 0.001, xbase, lSolver, whichmodel, hardcons, None, None, None, stratLam, scale, 1) correctlambda = 2.048585522856004e+02 correctY_radius = 2 self.assertAlmostEqual(Y_radius, correctY_radius, places=15) self.assertAlmostEqual(double(lambd), correctlambda, places=9) #Same test without the shift in interpolation points QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15) lambd, Y_radius = bcdfo_poisedness_Y_(QZ, RZ, Y, 0.001, xbase, lSolver, whichmodel, hardcons, None, None, None, stratLam, scale, 0) self.assertAlmostEqual(Y_radius, correctY_radius, places=15) self.assertAlmostEqual(double(lambd), correctlambda, places=11)
def test_bcdfo_hessP_2(self): """ Little bit more complicated tests initally written in the matlab code """ Y = array([[1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 2, 3]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15) model = (QZ.dot(np.linalg.solve(RZ.T, array([1, 2, 3, 4, 5, 6]).T))).T ans = bcdfo_hessP_(model, array([[0], [0]]), xbase, scale, 0) correctans = array([[4.0000, -0.5000], [-0.5000, 1.0000]]) self.assertTrue( compare_array(correctans, ans, self.abs_tol, self.rel_tol)) #Same test as above but with the shift in interpolation points Y = array([[1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 2, 3]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 1, 1, 1, 1e15) model = (QZ.dot(np.linalg.solve(RZ.T, array([[1, 2, 3, 4, 5, 6]]).T))).T ans = bcdfo_hessP_(model, array([[0], [0]]), xbase, scale, 1) correctans = array([[4.0000, -0.5000], [-0.5000, 1.0000]]) self.assertTrue( compare_array(correctans, ans, self.abs_tol, self.rel_tol))
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_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_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_find_new_yj(self): """ This is the test written in the Matlab Code. By Looking carefully, the result is slightly different because the sign of ynew[1] is opposite to the sign computed in matlab. This, after investigation, comes from a difference in the computation of pvalue & mvalue on approximately the fiftheen significant digit, which leads to mvalue < pvalue in python instead of mvalue > pvalue in matlab. It is interesting to keep this in memory, but this is not really a bug. """ Y = array([[3.0, 1.0, 0, 2.0, 1.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.01, 2.0]]) whichmodel = 0 QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 1, 1, 1, 1e15) ynew, improvement, msgTR = bcdfo_find_new_yj_(QZ, RZ, Y, 4, 1.0, 0.001, xbase, 1, whichmodel, scale, 1) correctynew = array([[3.280776988023534, -0.959774286524791]]).T correctimprovement = 314.8805392927235 self.assertAlmostEqual(correctimprovement, double(improvement), 11) self.assertTrue( compare_array(correctynew, ynew, self.abs_tol, self.rel_tol)) #Same test as above but without the shifting in the interpolation points Y = array([[3.0, 1.0, 0, 2.0, 1.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.01, 2.0]]) whichmodel = 0 QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 0, 1, 1, 1e15) ynew, improvement, msgTR = bcdfo_find_new_yj_(QZ, RZ, Y, 4, 1.0, 0.001, xbase, 1, whichmodel, scale, 0) correctynew = array([[3.280776988023534, -0.959774286524791]]).T correctimprovement = 314.8805392927235 self.assertAlmostEqual(correctimprovement, double(improvement), places=11) self.assertTrue( compare_array(correctynew, ynew, self.abs_tol, self.rel_tol))
def bcdfo_augment_Y_(Ynew=None, Y=None, whichmodel=None, shift_Y=None, Delta=None, normgx=None, kappa_ill=None, *args, **kwargs): """ # # Augment the interpolation set by adding new vector(s). This assumes that, # on entry, the polynomial is not yet fully quadratic. If this is the case, # the current interpolation set (and the associated polynomial degree) are # increased by the number of columns in Ynew and a new factorization of the # (possibly shifted) matrix is computed. # # INPUT: # # Ynew : the new vectors to add to the interpolation set # Y : a matrix whose columns contain the current interpolation points # whichmodel : kind of model to build # shift_Y : 0 if no shift in interpolation points, 1 otherwise # Delta : trust-region radius # normgx : infinity norm of the projected gradient # kappa_ill : threshold to declare a system matrix as ill-conditioned # # OUTPUT: # # p1 : the updated number of points in set Y # QZ, RZ : the QR factors of the (possibly shifted) matrix containing # the polynomial expansion of the updated interpolation points, # Y : a matrix whose columns contain the updated interpolation points # xbase : the updated base point, # scale : the updated model diagonal scaling. # # PROGRAMMING: Ph. Toint and A. Troeltzsch, April 2009. # (This version 12 IX 2010) # # DEPENDENCIES: bcdfo_build_QR_of_Y # # TEST: # Y = [ 0 1 0 2 0 ; 0 0 1 0 2 ]; # [ QZ, RZ, xbase, scale ] = bcdfo_build_QR_of_Y( Y, 0, 0, 1, 1, 1e15 ); # [ p1, QZ, RZ, Y, xbase, scale ] = bcdfo_augment_Y( [1;0.01], Y, 0, 0, 1, ... # 1, 1e15 ) # gives # p1 = # # 6 # # QZ = # # 1.0000 0 0 0 0 0 # 0 -0.8944 0 -0.4472 0 0 # 0 0 -0.8944 0 -0.4472 0 # 0 -0.4472 0 0.8944 0 0 # 0 0 -0.4472 0 0.8944 0 # 0 0 0 0 0 1.0000 # # RZ = # # 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 # 0 -1.1180 0 -2.6833 0 -1.1180 # 0 0 -1.1180 0 -2.6833 -0.0090 # 0 0 0 0.8944 0 0 # 0 0 0 0 0.8944 -0.0044 # 0 0 0 0 0 0.0100 # # Y = # # 0 1.0000 0 2.0000 0 1.0000 # 0 0 1.0000 0 2.0000 0.0100 # # xbase = # # 0 # 0 # # scale = # # 1 # 1 # 1 # 1 # 1 # 1 # # In the scaled case: # Y = [ 0 1 0 2 0 ; 0 0 1 0 2 ]; # [ QZ, RZ, xbase, scale ] = bcdfo_build_QR_of_Y( Y, 0, 1, 1, 1, 1e15 ); # [ p1, QZ, RZ, Y, xbase, scale ] = bcdfo_augment_Y( [1;0.01], Y, 0, 1, 1, ... # 1, 1e15 ) # gives # p1 = # # 6 # # QZ = # # 1.0000 0 0 0 0 0 # 0 -0.9701 0 -0.2425 0 0 # 0 0 -0.9701 0 -0.2425 0 # 0 -0.2425 0 0.9701 0 0 # 0 0 -0.2425 0 0.9701 0 # 0 0 0 0 0 1.0000 # # RZ = # # 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 # 0 -0.5154 0 -1.0914 0 -0.5154 # 0 0 -0.5154 0 -1.0914 -0.0049 # 0 0 0 0.2425 0 0 # 0 0 0 0 0.2425 -0.0012 # 0 0 0 0 0 0.0025 # # Y = # # 0 1.0000 0 2.0000 0 1.0000 # 0 0 1.0000 0 2.0000 0.0100 # # xbase = # # 0 # 0 # # scale = # # 1.0000 # 0.5000 # 0.5000 # 0.2500 # 0.2500 # 0.2500 # # CONDITIONS OF USE: Use at your own risk! No guarantee of any kind given. # """ # varargin = cellarray(args) # nargin = 7-[Ynew,Y,whichmodel,shift_Y,Delta,normgx,kappa_ill].count(None)+len(args) n, p1 = size_(Y, nargout=2) if ((p1 >= ((n + 1) * (n + 2)) / 2) and (whichmodel != 3)): disp_( ' === augment_Y: warning!!! The interpolation is already fully quadratic!' ) disp_(' Ignoring augmentation...') QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, shift_Y, Delta, normgx, kappa_ill, nargout=4) else: Y = concatenate_([Y, Ynew], axis=1) p1 = p1 + size_(Ynew, 2) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, shift_Y, Delta, normgx, kappa_ill, nargout=4) return p1, QZ, RZ, Y, xbase, scale
def bcdfo_replace_in_Y_(QZ=None, RZ=None, ynew=None, Y_=None, j=None, xbase=None, whichmodel=None, scale=None, shift_Y=None, Delta=None, normgx=None, kappa_ill=None, *args, **kwargs): """ # # Updates the interpolation set for a transformation of Y in Yplus where the # vector ynew replaces Y(:,j). Also update the factorization of Z(Y) # accordingly. # # INPUT: # # QZ : the Q matrix of the QR decomposition of Z(Y) # RZ : the R matrix of the QR decomposition of Z(Y) # Y : the current interpolation set (by columns) # ynew : the vector hich is to replace Y(:,j) in the interpolation set # j : the index of the interpolation point to be replaced # xbase : the current base point # whichmodel : kind of model to build # scale : the current interpolation set scaling # shift_Y : 0 if no shift in interpolation points, 1 otherwise # Delta : trust-region radius # normgx : infinity norm of the projected gradient # kappa_ill : threshold to declare a system matrix as ill-conditioned # # OUTPUT: # # QZ : the updated Q matrix of the QR decomposition of Z(Y) # RZ : the updated R matrix of the QR decomposition of Z(Y) # Y : the updated Y # xbase : the base point after the replacement # scale : the interpolation set scaling after the replacement # # PROGRAMMING: Ph. Toint, S. Gratton, A. Troeltzsch April 2009. # (This version 15 IX 2010) # # USES: bcdfo_build_QR_of_Y # # TEST: # Y = [ 1 1 0 0 0 -1; 1 0 -1 1 0 0 ]; # [QZ,RZ,xbase,scale] = bcdfo_build_QR_of_Y( Y , 0, 0, 1, 1, 1e15 ); # ynew = 0.25*Y(:,3); # [ QZplus, RZplus, Yplus ] = bcdfo_replace_in_Y( QZ, RZ, ynew, Y, 3, ... # xbase, whichmodel, scale, 0, 1, 1, 1e15) # QZplus = # # -0.4714 -0.4714 0.7205 0.1527 0.1149 0.0000 # -0.4714 -0.4714 -0.5764 -0.1221 -0.0919 0.4472 # -0.4714 0.4714 -0.1891 0.6333 0.3446 0.0000 # -0.2357 -0.2357 -0.2882 -0.0611 -0.0459 -0.8944 # -0.2357 0.2357 0.1081 0.1813 -0.9189 -0.0000 # -0.4714 0.4714 0.1351 -0.7240 0.1149 -0.0000 # # RZplus = # # -2.1213 -1.0607 -0.3609 -1.0607 -0.4714 -0.1179 # 0 -1.0607 -0.5819 0.1179 -0.4714 -0.1179 # 0 0 0.7711 0.5854 0.7205 1.1527 # 0 0 0 0.8766 0.1527 0.2442 # 0 0 0 0 0.1149 0.1838 # 0 0 0 0 0 -0.8944 # # Yplus = # # 1.0000 1.0000 0 0 0 -1.0000 # 1.0000 0 -0.2500 1.0000 0 0 # # The scaled version: # Y = [ 1 1 0 0 0 -1; 1 0 -1 1 0 0 ]; # [QZ,RZ,xbase,scale] = bcdfo_build_QR_of_Y( Y , 0, 1, 1, 1, 1e15 ); # ynew = 0.25*Y(:,3); # [ QZplus, RZplus, Yplus ] = bcdfo_replace_in_Y( QZ, RZ, ynew, Y, 3, ... # xbase, whichmodel, scale, 1, 1, 1, 1e15 ) # QZplus = # # -1.0000 0 0 0 0 0 # 0 0.0000 -0.8552 0.4700 0.0000 0.2182 # 0 -0.9759 0.0127 0.0232 -0.2166 0.0000 # 0 -0.0000 0.1912 -0.1051 0.0000 0.9759 # 0 0.2182 0.0569 0.1036 -0.9687 0.0000 # 0 -0.0000 0.4781 0.8699 0.1211 0.0000 # # RZplus = # # -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 # 0 0.4583 0.5796 -0.0000 0.4583 0.4583 # 0 0 0.5229 0.4016 0.4972 1.0327 # 0 0 0 -0.2207 -0.0467 -0.1145 # 0 0 0 0 0.0242 0.0484 # 0 0 0 0 0 0.1952 # # Yplus = # # 1.0000 1.0000 0 0 0 -1.0000 # 1.0000 0 -0.2500 1.0000 0 0 # """ # varargin = cellarray(args) # nargin = 12-[QZ,RZ,ynew,Y,j,xbase,whichmodel,scale,shift_Y,Delta,normgx,kappa_ill].count(None)+len(args) Y = copy(Y_) # replace new point in Y Y[:, j] = ynew.reshape(-1) # a new factorization must be computed. QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, shift_Y, Delta, normgx, kappa_ill, nargout=4) return QZ, RZ, Y, xbase, scale
def test_bcdfo_repair_Y(self): """ This is the test written in the Matlab Code. Results are the same except for a few signs due to a non-unique QR decomposition. """ Y = array([[0, 1, 0, 2, 1, 0], [0, 0, 1, 0, 0.01, 2]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15) QZplus, RZplus, Yplus, replaced, maximprove, Y_radius, xbase, scale = bcdfo_repair_Y_( QZ, RZ, Y, 0.7, 10, 1.0e-10, 1.1, 0.001, xbase, 1, 0, 0, array([-10, -10]), array([10, 10]), array([1, 2]), 1, scale, 0, 1, 1e15) correctQZplus = array( [[1, 0, 0, 0, 0, 0], [ 0, -0.659566419393763, 0.583075869829704, -0.259014354637790, 0.383735140707789, 0.103216153328673 ], [ 0, -0.640135734527690, -0.703005089235313, -0.116047869763477, -0.172250116473354, 0.229941025457003 ], [ 0, 0.165662796629888, 0.0580981831826901, -0.900707614268715, -0.383735140707789, -0.103216153328673 ], [ 0, 0.156045791159224, 0.217778643032798, 0.0539460623491271, -0.274281054964081, 0.921998860598591 ], [ 0, 0.321564812538689, -0.339121568587501, -0.324438086497736, 0.774980311901311, 0.275205518205477 ]]) correctRZplus = array( [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000], [ 0, 0.761619249046136, 0.120705803297469, -0.987807245527749, 0.141209675627013, -0.968179886736932 ], [ 0, 0, 0.747318893145360, 1.28234810602479, -0.504495501740099, -0.970452892405031 ], [ 0, 0, 0, -2.31944393781301, 0.0241326579145779, -0.124203614828700 ], [0, 0, 0, 0, -0.545860227607381, -0.893062342874869], [0, 0, 0, 0, 0, 2.30387977211119]]) correctYplus = array([[ 0, -0.502338481034726, 0.356130119179943, 2, -0.603012769702487, 0 ], [ 0, -0.487539697418576, -0.602637083218470, 0, 0.355493490030523, 2 ]]) correctreplaced = array([1, 2, 4]) correctmaximprove = 1.000153015137598 correctY_radius = 2 self.assertTrue( compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctreplaced, replaced, self.abs_tol, self.rel_tol)) self.assertAlmostEqual(correctmaximprove, double(maximprove), places=13) self.assertEqual(correctY_radius, Y_radius) #The scaled version (i.e. with shift in interpolation points) Y = array([[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, 1, 1, 1e15) QZplus, RZplus, Yplus, replaced, maximprove, Y_radius, xbase, scale = bcdfo_repair_Y_( QZ, RZ, Y, 0.7, 10, 1.0e-10, 1.1, 0.001, xbase, 1, 0, 0, array([-10, -10]), array([10, 10]), array([1, 2]), 1, scale, 1, 1, 1e15) correctQZplus = array( [[1.0000, 0, 0, 0, 0, 0], [ 0, -0.701665921132481, 0.663289830649876, -0.149898529361902, 0.206022638530179, 0.0528832580876775 ], [ 0, -0.680994993999292, -0.714381653387058, -0.0589306015912921, -0.0924789168850424, 0.117811313462405 ], [ 0, 0.0881184482538805, 0.0261672408486609, -0.900323579771228, -0.412045277060358, -0.105766516175355 ], [ 0, 0.0830030233294948, 0.111907596144591, 0.0351764352940761, -0.294515944525440, 0.944780485014161 ], [ 0, 0.171044995438928, -0.191042977604362, -0.402787394538547, 0.832154655348015, 0.282005557794224 ]]) correctRZplus = array( [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000], [ 0, 0.357961293192034, 0.0762419683750912, -0.657606697005541, 0.0866619532593967, -0.639493482334544 ], [ 0, 0, 0.349110509250466, 0.676373451074206, -0.313769619685537, -0.658427855314762 ], [ 0, 0, 0, -0.600060319247516, 0.0159399616505447, -0.0413423839442541 ], [0, 0, 0, 0, -0.146532784415969, -0.239736889147762], [0, 0, 0, 0, 0, 0.590201555969485]]) correctYplus = array([[ 0, -0.502338481034726, 0.356130119179943, 2, -0.603012769702487, 0 ], [ 0, -0.487539697418576, -0.602637083218470, 0, 0.355493490030523, 2 ]]) correctreplaced = array([1, 2, 4]) # changed to python indices correctmaximprove = 1.000153015137598 correctY_radius = 2 self.assertTrue( compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctreplaced, replaced, self.abs_tol, self.rel_tol)) self.assertAlmostEqual(correctmaximprove, double(maximprove), places=13) self.assertEqual(correctY_radius, Y_radius)
def test_bcdfo_include_in_Y(self): # TEST: """ This is the test written in the Matlab Code. Results are the same except for a few signs due to a non-unique QR decomposition """ Y = array([[0, 1, 0, 2, 1, 0], [0, 0, 1, 0, 0.01, 2]]) whichmodel = 0 QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 1, 1, 1, 1e15) QZplus, RZplus, Yplus, pos, xbase, scale = bcdfo_include_in_Y_( array([[-1, 1]]).T, QZ, RZ, Y, array(list(range(2, 6))), 0.01, 'weighted', xbase, whichmodel, 0, scale, 1, 1, 1, 1e15) #print QZplus, RZplus, Yplus, pos, xbase, scale correctQZplus = array([[1.0000, 0, 0, 0, 0, 0], [ 0, -9.701425001453321e-01, 0.0000, -2.425356250363330e-01, 0.0000, -0.0000 ], [ 0, 0.0000, -9.701425001453321e-01, -0.0000, 0.0000, -2.425356250363330e-01 ], [ 0, -2.425356250363330e-01, -0.0000, 9.701425001453319e-01, -0.0000, 0.0000 ], [ 0, 0.0000, -2.425356250363330e-01, 0.0000, -0.0000, 9.701425001453319e-01 ], [0, -0.0000, 0.0000, 0.0000, 1.0000, 0.0000]]) correctRZplus = array( [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000], [ 0, -5.153882032022076e-01, 0, -1.091410312663498e+00, 4.547542969431244e-01, 0.0000 ], [ 0, 0, -5.153882032022076e-01, -0.0000, -5.153882032022077e-01, -1.091410312663498e+00 ], [0, 0, 0, 2.425356250363330e-01, 2.425356250363330e-01, -0.0000], [0, 0, 0, 0, -0.2500, -0.0000], [0, 0, 0, 0, 0, 2.425356250363330e-01]]) correctYplus = array([[0, 1, 0, 2, -1, 0], [0, 0, 1, 0, 1, 2]]) correctpos = 4 correctxbase = array([[0], [0]]) correctscale = array([ [1.0000], [0.5000], [0.5000], [0.2500], [0.2500], [0.2500], ]) self.assertTrue( compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol)) self.assertEqual(correctpos, pos) self.assertTrue( compare_array(correctxbase, xbase, self.abs_tol, self.rel_tol)) self.assertTrue( compare_array(correctscale, scale, self.abs_tol, self.rel_tol)) var = np.dot( QZplus.T, bcdfo_evalZ_( (Yplus - np.dot(Y[:, 0].reshape(-1, 1), ones_(1, 6))) * scale[1], 6)) ident = np.linalg.solve(var, RZplus) self.assertTrue( compare_array(array(ident), array(np.eye(6)), self.abs_tol, self.rel_tol))
def sqpdfo_prelim_(func_=None,x0_=None,lm0_=None,Delta0_=None,lb_=None,ub_=None,\ scaleX_=None,scalefacX_=None,cur_degree_=None,rep_degree_=None,plin_=None,\ pdiag_=None,pquad_=None,c_=None,initial_Y_=None,kappa_ill_=None,\ factor_FPR_=None,Lambda_FP_=None,Lambda_CP_=None,eps_L_=None,lSolver_=None,\ hardcons_=None,stratLam_=None,xstatus_=None,sstatus_=None,dstatus_=None,\ options_=None,*args,**kwargs): ############################################################################### # This function realizes the following preliminary jobs: # - build initial poised interpolation set # - check the bounds and that initial point inside the bounds # - check the given options # - compute function and constraint values # - compute initial multipliers (if not given) # - initial printings ############################################################################### func = copy(func_) x0 = copy(x0_) lm0 = copy(lm0_) Delta0 = copy(Delta0_) lb = copy(lb_) ub = copy(ub_) scaleX = copy(scaleX_) scalefacX = copy(scalefacX_) cur_degree = copy(cur_degree_) rep_degree = copy(rep_degree_) plin = copy(plin_) pdiag = copy(pdiag_) pquad = copy(pquad_) c = copy(c_) initial_Y = copy(initial_Y_) kappa_ill = copy(kappa_ill_) factor_FPR = copy(factor_FPR_) Lambda_FP = copy(Lambda_FP_) Lambda_CP = copy(Lambda_CP_) eps_L = copy(eps_L_) lSolver = copy(lSolver_) hardcons = copy(hardcons_) stratLam = copy(stratLam_) xstatus = copy(xstatus_) sstatus = copy(sstatus_) dstatus = copy(dstatus_) options = copy(options_) info = dummyUnionStruct() info.nsimul = array([]) nbr_slacks = glob.get_nbr_slacks() sl = glob.get_slacks() Y = array([]) gamma1 = 0.010000000000000 eps = 2.220446049250313e-16 stallfact = 10 * eps nfix = None indfix = None xfix = None vstatus = None QZ = None RZ = None scale = None poised = None Y_radius = None poised_model = None X = None fX = None #Y = None fY = None ciX = None ciY = None ceX = None ceY = None poisedness_known = None m = None normgx = None fcmodel = None ind_Y = None i_xbest = None indfree = None # Set output arguments n = 0 nb = 0 mi = 0 me = 0 lm = array([]) info.f = np.NaN info.ce = array([]) info.g = [] info.ai = [] info.ae = [] info.hl = [] info.niter = 0 info.glagn = float('NaN') info.feasn = float('NaN') info.compl = float('NaN') shift_Y = 1 x = copy(np.NaN) fx = copy(np.NaN) gx = copy(np.NaN) # check user options or get default values if no values given by the user info, options, values = sqpdfo_options_(info, options, nargout=3) if info.flag: return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,xstatus,\ sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,Y,fY,\ ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,i_xbest,\ cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values info.nsimul = np.zeros(values.nsimultype) whichmodel = options.whichmodel # Check the argument x0; deduce n n = size_(x0, 1) if size_(x0, 2) != 1: if options.verbose: fprintf_( options.fout, '### sqpdfo_prelim: the initial x must be an n-vector\n\n') info.flag = values.fail_on_argument return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,xstatus,\ sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,Y,fY,\ ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,i_xbest,\ cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values if n < 1: if options.verbose: fprintf_( options.fout, '### sqpdfo_prelim: the initial x must be an n-vector with n > 0\n\n' ) info.flag = values.fail_on_argument return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,xstatus,\ sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,Y,fY,\ ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,i_xbest,\ cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values # Compute the number of bounds if isempty_(lb): lb = -options.inf * ones_(n + mi, 1) if isempty_(ub): ub = options.inf * ones_(n + mi, 1) nb_lo = sum_(lb[0:n] > -options.inf) nb_up = sum_(ub[0:n] < options.inf) nb = sum_(min_((lb[0:n] > -options.inf) + (ub[0:n] < options.inf), 1)) # Checking the bounds and correct Delta0 if there is insufficient space # between the bounds. Modification of x0 if construction of first # interpolation model would interfere with given bounds. zero = 0.0 nfix = 0 indfix = array([]) xfix = zeros_(n, 1) vstatus = zeros_(n, 1) temp = zeros_(n, 1) for j in range(0, n): # Check lower and upper bounds. if (lb[j] > ub[j]): disp_('Error: Lower bound of component ', str(j), ' exceeds upper bound !!') info.flag = 2 return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,Y,fY,\ ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,i_xbest,\ cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values # Check difference between bounds. temp[j] = ub[j] - lb[j] if (temp[j] < Delta0 + Delta0): if (temp[j] == zero): nfix = nfix + 1 indfix = concatenate_([indfix, j], axis=1) vstatus[j] = c.alwaysfixed xfix[j] = lb[j] continue else: Delta0 = 0.5 * temp[j] disp_(' Diff. between lower and upper bound of component ',str(j),\ ' is less than 2*Delta0 !! New Delta0=',str(Delta0)) # Move the starting point inside the bounds if necessary templ = lb[j] - x0[j] tempu = ub[j] - x0[j] if (templ >= -Delta0): x0[j] = lb[j] + Delta0 else: if (tempu <= Delta0): x0[j] = ub[j] - Delta0 # Scale x0 and bounds if user-defined. if (scaleX): for i in range(0, n): if (scalefacX[i] > 0): x0[i] = x0[i] * scalefacX[i] lb[i] = lb[i] * scalefacX[i] ub[i] = ub[i] * scalefacX[i] else: scalefacX[i] = 1 # Reset constants if fixed some variables. if (nfix > 0): nfree = n - nfix if (nfree <= 0): disp_('No free variables. Please, enlarge search space!') info.flag = 2 return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,Y,fY,\ ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,i_xbest,\ cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values indfree = setdiff_(arange(0, n), indfix) x0 = x0[indfree] if (cur_degree == plin): cur_degree = nfree + 1 else: if (cur_degree == pdiag): cur_degree = 2 * nfree + 1 else: if (cur_degree == pquad): cur_degree = ((nfree + 1) * (nfree + 2)) / 2 if (rep_degree == plin): rep_degree = nfree + 1 else: if (rep_degree == pdiag): rep_degree = 2 * nfree + 1 else: if (rep_degree == pquad): rep_degree = ((nfree + 1) * (nfree + 2)) / 2 plin = nfree + 1 pdiag = 2 * nfree + 1 pquad = ((nfree + 1) * (nfree + 2)) / 2 n = copy(nfree) else: indfree = arange(0, n) x = copy(x0) # Compute an interpolation set around x0 and compute f, ci, ce, g, ai, ae getfY = 1 while (getfY): if (options.verbose > 2): disp_(' Degree of the initial model = ', str(cur_degree)) # Compute an initial poised interpolation set around the starting point. if initial_Y == 'random': Y[:, 0] = x0 # Loop in case of an accidentally ill-conditioned initial system ill_init = 1 while (ill_init): Y[:, 1:cur_degree] = -ones_(n, cur_degree - 1) + 2 * rand_( n, cur_degree - 1) for j in range(1, cur_degree): Y[:, j] = Y[:, 0] + Y[:, j] * (Delta0 / norm_(Y[:, j])) QZ,RZ,x,scale=\ bcdfo_build_QR_of_Y_(Y,whichmodel,shift_Y,Delta0,1,kappa_ill,nargout=4) # check the condition if (cond_(RZ) < kappa_ill): ill_init = 0 # Make the set poised. QZ,RZ,Y,replaced,poised,Y_radius,x,scale=\ bcdfo_repair_Y_(QZ,RZ,Y,Delta0,factor_FPR,Lambda_FP,Lambda_CP,eps_L,x,\ lSolver,whichmodel,hardcons,lb,ub,indfree,stratLam,scale,shift_Y,\ 1,kappa_ill,nargout=8) poisedness_known = 1 elif initial_Y == 'simplx': # Compute the initial interpolation set (simplex plus midpoints). I = eye_(n) Y = zeros_(n, n + 1) Y[:, 0] = x0.reshape(-1) for j in range(0, n): # initial degree is linear step1 = -Delta0 Y[:, j + 1] = x0.reshape(-1) + step1 * I[:, j] if (cur_degree >= pdiag): # initial degree is diagonal step2 = copy(Delta0) Y[:, j + 1 + n] = x0.reshape(-1) + step2 * I[:, j] # if (cur_degree == pquad): # initial degree is quadratic # k=2 * n + 2 # for j in range(0,n): # for jj in range(j + 1,n+1): # Y[:,k-1]=0.5 * (Y[:,j + 1] + Y[:,jj + 1]) # k=k + 1 # Build the initial factorization. QZ,RZ,x,scale=\ bcdfo_build_QR_of_Y_(Y,whichmodel,shift_Y,Delta0,1,kappa_ill,nargout=4) poised,Y_radius=\ bcdfo_poisedness_Y_(QZ,RZ,Y,eps_L,x,1,whichmodel,hardcons,lb,ub,indfree,\ stratLam,scale,shift_Y,nargout=2) poisedness_known = 1 poised_model = 1 # The initial interpolation set is poised. # Compute the associated function values, possibly reducing Delta0 to # ensure that the objective function remains finite at all interpolation # points. X = array([]) fX = array([]) ciX = array([]) ceX = array([]) ind_Y = array([]) info.ci = array([]) info.ce = array([]) for i in range(0, cur_degree): X,fX,ciX,ceX,neval,xstatus,sstatus,dstatus,info,outdic=\ sqpdfo_augmX_evalf_(func,Y[:,[i]],i,X,fX,ciX,ceX,nfix,xfix,indfix,\ indfree,1e+25,info.nsimul[1],xstatus,c.inY,sstatus,\ dstatus,scaleX,scalefacX,info,options,values,nargout=9) if info.flag: return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,\ vstatus,xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,\ poised_model,X,fX,Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,\ gx,normgx,fcmodel,ind_Y,i_xbest,cur_degree,rep_degree,plin,pdiag,\ pquad,indfree,info,options,values # If the computed function value is infinite, restart with a smaller Delta0. if (abs(fX[i]) > 1e+25): break # All functions values at points of Y are finite. No need for # another pass. if (i == cur_degree - 1): getfY = 0 ind_Y = concatenate_([ind_Y, array([i])], axis=1) # Check for infinity constraint values ceX[ceX >= 1e25] = 10 * max_(ceX[ceX < 1e25]) ceX[ceX <= -1e25] = 10 * min_(ceX[ceX > -1e25]) fY = copy(fX) ciY = copy(ciX) ceY = copy(ceX) xstatus = xstatus.T dstatus = dstatus.T # Another pass is needed with a smaller Delta0 (at least one function # value is infinite). if (getfY): # Decrease Delta0. Delta0 = gamma1 * Delta0 # Terminate with an error message if Delta0 becomes negligible wrt x0. if (Delta0 < stallfact * norm_(x0)): disp_('Error: cannot find enough finite objective function values',\ ' in the neighbourhood of the starting point! Terminating.') # including fixed variables at return if (nfix > 0): I = eye_(n + nfix) x = I[:, indfix] * zeros_(nfix, 1) + I[:, indfree].dot(x) gx = I[:, indfix] * zeros_(nfix, 1) + I[:, indfree].dot(gx) return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,\ vstatus,xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,\ poised_model,X,fX,Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,\ gx,normgx,fcmodel,ind_Y,i_xbest,cur_degree,rep_degree,plin,\ pdiag,pquad,indfree,info,options,values fx0 = copy(fY[0]) info.f = fx0 m = copy(cur_degree) - 1 i_xbest = 0 # Move to the best point in the interpolation set, if different from x0. # ATTENTION for constrained problems...! Check merit function value! #[x, fx, QZ, RZ, Y, fY, ciY, ceY, ind_Y, i_xbest, scale] = ... # sqpdfo_find_smallf(c, QZ, RZ, Y, fY, ciY, ceY,... # 1:cur_degree, 1, cur_degree, indfree, x, lb, ub, fx0, ... # zeros(cur_degree,1), whichmodel, scale, shift_Y, Delta0, 1, kappa_ill); # Compute the associated polynomial interpolation model(s) # for objective and possible constraints. initmodel = zeros_(1, pquad) rhsY = concatenate_([fY.reshape(1, -1), ciY, ceY], axis=0) fcmodel=\ bcdfo_computeP_(QZ,RZ,Y,rhsY,whichmodel,initmodel,ind_Y,0,0,gx,scale,shift_Y,Delta0) gx = bcdfo_gradP_(fcmodel[[0], :], x, x, scale, shift_Y) normgx, _ = bcdfo_projgrad_(n, x, gx, lb[indfree], ub[indfree]) if any_(size_(gx) != [n, 1]): if options.verbose: fprintf_(options.fout,'### sqpdfo: the computed gradient g has a wrong ',\ 'size, (%0i,%0i) instead of (%0i,1)\n\n'%(size_(gx),n)) info.flag = values.fail_on_simul return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,\ Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,\ i_xbest,cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values info.g = gx # Check constraints and deduce mi and me mi = size_(ciY, 1) if mi > 0: info.ci = copy(ciY[:, [0]]) # compute model gradient associated with the inequality constraints gci = zeros_(mi, n) for i in range(0, mi): gci[i, :] = bcdfo_gradP_(fcmodel[[1 + i], :], x, x, scale, shift_Y).T info.ai = copy(gci) else: info.ci = array([]) info.ai = array([]) me = size_(ceY, 1) if me > 0: info.ce = copy(ceY[:, [0]]) # compute model gradient associated with the equality constraints gce = zeros_(me, n) for i in range(0, me): gce[i, :] = bcdfo_gradP_(fcmodel[[1 + mi + i], :], x, x, scale, shift_Y).T info.ae = gce else: info.ce = array([]) info.ae = array([]) # Initialize slack variables if necessary if nbr_slacks > 0: ce = info.ce for i in range(0, nbr_slacks): if ce[-nbr_slacks + i] > 0: sl[i] = min_(sqrt_(ce[-nbr_slacks + i]), sqrt_(ub[-nbr_slacks + i])) else: sl[i] = 0 glob.set_slacks(sl) # Initial printing (1) fprintf_('\n') fprintf_( '**************************************************************************************\n' ) fprintf_( '* *\n' ) fprintf_( '* SQPDFO: Sequential-Quadratic-Programming Derivative-Free Optimization *\n' ) fprintf_( '* *\n' ) fprintf_( '* (c) A. Troeltzsch, 2013-2019 *\n' ) fprintf_( '* *\n' ) fprintf_( '**************************************************************************************\n' ) fprintf_('\n') values.dline = '-------------------------------------------' values.dline = strcat_(values.dline, values.dline) # 80 dashes values.eline = '===========================================' values.eline = strcat_(values.eline, values.eline) # 80 '=' characters values.sline = '*******************************************' values.sline = strcat_(values.sline, values.sline) # 80 '*' characters if options.verbose > 0 and options.verbose < 4: fprintf_(options.fout, ' iter neval fvalue merit ') if (me > 0): fprintf_(options.fout, ' |grad Lag| feasibility') else: fprintf_(options.fout, ' gradient ') fprintf_(options.fout, ' delta stepsize') if options.hess_approx == values.bfgs: fprintf_(options.fout, ' BFGS\n') else: fprintf_(options.fout, ' \n') fprintf_(options.fout, ' \n') if options.verbose >= 4: fprintf_(options.fout, '\nSQPDFO optimization solver\n\n') if isinstance(func, types.FunctionType): func_name = str(func) else: func_name = copy(func) fprintf_(options.fout, ' function name: "%s"\n' % (func_name)) fprintf_(options.fout, ' dimensions:\n') fprintf_(options.fout, ' . variables (n): %4i\n' % (n)) if nb > 0: fprintf_( options.fout, ' . bounds on variables (nb): (%0i lower, %0i double, %0i upper)\n' % (nb, nb_lo, nb_up)) if mi > 0: fprintf_(options.fout, ' . inequality constraints (mi): %4i\n' % (mi)) if me > 0: fprintf_(options.fout, ' . equality constraints (me): %4i\n' % (me)) fprintf_(options.fout, ' required tolerances for optimality:\n') if nb + mi + me > 0: fprintf_( options.fout, ' . gradient of the Lagrangian %8.2e\n' % (options.tol_grad)) fprintf_( options.fout, ' . feasibility %8.2e\n' % (options.tol_feas)) else: fprintf_( options.fout, ' . gradient of the cost function %8.2e\n' % (options.tol[0])) fprintf_(options.fout, ' counters:\n') fprintf_(options.fout, ' . max iterations %4i\n' % (options.miter)) fprintf_( options.fout, ' . max function evaluations %4i\n' % (options.msimul)) fprintf_(options.fout, ' algorithm:\n') fprintf_(options.fout, ' . globalization by trust regions\n') fprintf_(options.fout, ' various input/initial values:\n') if (nb + mi + me == 0) and (options.df1 > 0) and (info.f > 0): fprintf_( options.fout, ' . expected initial decrease %8.2e\n' % (options.df1 * info.f)) if nb + mi > 0: fprintf_( options.fout, ' . infinite bound threshold %8.2e\n' % (options.inf)) fprintf_(options.fout, ' . |x|_2 %8.2e\n' % (norm_(x))) # Compute an initial multiplier if not given (takes a while); info.ci must be known if (nb + mi + me > 0): if isempty_(lm0): lm, info = sqpdfo_compute_multiplier_(x, [], [], info, options, values, nargout=2) if info.flag: return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,\ Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,\ i_xbest,cur_degree,rep_degree,plin,pdiag,pquad,indfree,\ info,options,values if options.verbose >= 4: fprintf_( options.fout, ' . |lm|_2 %8.2e (default: least-squares value)\n' % (norm_(lm))) else: lm = copy(lm0) if options.verbose >= 4: fprintf_( options.fout, ' . |lm|_2 %8.2e\n' % (norm_(lm))) # Initial optimality feas,compl,info=\ sqpdfo_optimality_(x,lm,lb[indfree],ub[indfree],info,options,nargout=3) if info.flag: return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,\ X,fX,Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,\ ind_Y,i_xbest,cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values # Initial printing (2) if options.verbose >= 4: fprintf_(options.fout,' . |g|_inf %8.2e\n'\ %(norm_(info.g,inf))) if nb + mi + me > 0: fprintf_(options.fout,' . |glag|_inf %8.2e\n'\ %(norm_(info.glag,inf))) if nb: fprintf_(options.fout,' . |x^#|_inf %8.2e\n'\ %(norm_(feas[0:n],inf))) if mi: fprintf_(options.fout,' . |ci^#|_inf %8.2e\n'\ %(norm_(feas[n:n + mi],inf))) if me: fprintf_(options.fout,' . |ce|_inf %8.2e\n'\ %(norm_(feas[n + mi:n + mi + me],inf))) fprintf_(options.fout, ' tunings:\n') fprintf_(options.fout,' . printing level %0i\n'\ %(options.verbose)) if info.flag: return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,\ Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,\ i_xbest,cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values return n,nb,mi,me,x,lm,lb,ub,scalefacX,Delta0,nfix,indfix,xfix,vstatus,\ xstatus,sstatus,dstatus,QZ,RZ,scale,poised,Y_radius,poised_model,X,fX,\ Y,fY,ciX,ciY,ceX,ceY,poisedness_known,m,gx,normgx,fcmodel,ind_Y,\ i_xbest,cur_degree,rep_degree,plin,pdiag,pquad,indfree,info,options,values
def test_bcdfo_augment_Y(self): """ This is the test written in the Matlab Code. Results are the same except for a few signs due to a non-unique QR decomposition """ Y = array([[ 0, 1, 0, 2, 0], [0, 0, 1, 0, 2 ]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 0, 1, 1, 1e15 ) p1, QZ, RZ, Y, xbase, scale = bcdfo_augment_Y_( array([[1, 0.01]]).T, Y, 0, 0, 1, 1, 1e15 ) #print "p1, QZ, RZ, Y, xbase, scale", p1, QZ, RZ, Y, xbase, scale correctQZ=array([ [1, 0, 0, 0, 0, 0], [0, -0.894427190999916, 0, -0.447213595499958, 0, 0], [0, 0, -0.894427190999916, 0, -0.447213595499958, 0], [0, -0.447213595499958, 0, 0.894427190999916, 0, 0], [0, 0, -0.447213595499958, 0, 0.894427190999916, 0], [0, 0, 0, 0, 0, 1]]) correctRZ=array([ [1, 1, 1, 1, 1, 1], [0, -1.11803398874990 , 0, -2.68328157299975, 0, -1.11803398874990], [0, 0, -1.11803398874990, 0, -2.68328157299975, -0.00896663258977416], [0, 0, 0, 0.894427190999916 , 0, 0], [0, 0, 0, 0, 0.894427190999916, -0.00442741459544958], [0, 0, 0, 0, 0, 0.0100000000000000]]) correctY = array([ [0, 1.0000, 0, 2.0000, 0, 1.0000], [0, 0, 1.0000, 0, 2.0000, 0.0100]]) self.assertEqual(p1, 6) self.assertTrue(compare_array(correctQZ, QZ, self.abs_tol, self.rel_tol)) self.assertTrue(compare_array(correctRZ, RZ, self.abs_tol, self.rel_tol)) self.assertTrue(compare_array(correctY, Y, self.abs_tol, self.rel_tol)) Y = array([[ 0, 1, 0, 2, 0], [0, 0, 1, 0, 2 ]]) QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1, 1, 1e15 ) p1, QZ, RZ, Y, xbase, scale = bcdfo_augment_Y_( array([[1, 0.01]]).T, Y, 0, 1, 1, 1, 1e15 ) #print "p1, QZ, RZ, Y, xbase, scale", p1, QZ, RZ, Y, xbase, scale correctQZ=array([ [1, 0, 0, 0, 0, 0], [0, -9.701425001453321e-01, 0, -2.425356250363330e-01, 0, 0], [0, 0, -9.701425001453321e-01, 0, -2.425356250363330e-01, 0], [0, -2.425356250363330e-01, 0, 9.701425001453319e-01, 0, 0], [0, 0, -2.425356250363330e-01, 0, 9.701425001453319e-01, 0], [0, 0, 0, 0, 0, 1]]) correctRZ=array([ [1, 1, 1, 1, 1, 1], [0, -5.153882032022076e-01, 0, -1.091410312663498, 0, -0.515388203202208], [0, 0, -5.153882032022076e-01, 0, -1.091410312663498, -0.00485374419603962], [0, 0, 0, 2.425356250363330e-01 , 0, 0], [0, 0, 0, 0, 2.425356250363330e-01,- 0.00120055134392985], [0, 0, 0, 0, 0, 0.00250000000000000]]) correctY = array([ [0, 1.0000, 0, 2.0000, 0, 1.0000], [0, 0, 1.0000, 0, 2.0000, 0.0100]]) self.assertEqual(p1, 6) self.assertTrue(compare_array(correctQZ, QZ, self.abs_tol, self.rel_tol)) self.assertTrue(compare_array(correctRZ, RZ, self.abs_tol, self.rel_tol)) self.assertTrue(compare_array(correctY, Y, self.abs_tol, self.rel_tol))