x = uqtkarray.dblArray2D() w = uqtkarray.dblArray1D() # create instance of quad class and output # points and weights print('Create an instance of Quad class') ndim = 2 level = 3 q = uqtkquad.Quad('LU','sparse',ndim,level,0,1) print('Now set and get the quadrature rule...') q.SetRule() q.GetRule(x,w) # print out x and w print('Displaying the quadrature points and weights:\n') x_np = uqtk2numpy(x) print(x_np) n = len(x) print('Number of quad points is ', n, '\n') # plot the quadrature points print('Plotting the points (get points in column major order as a flattened vector)') print('need to use reshape with fortran ordering') xpnts = zeros((n,ndim)) x.getnpdblArray(xpnts) # plot(xpnts[:,0], xpnts[:,1],'ob',ms=10,alpha=.25) # show() # convert the quad weights to numpy arrays w_np = zeros(n) w.getnpdblArray(w_np)
resulting numpy array is *only* row major (C contiguous) ''' # create numpy matrix and show flags a_np = np.array([[0, 2.00], [0.1, 1], [1, 5.0]]) print("flags for a_np to show whether C or F contiguous") print(a_np.flags) # get a uqtk array from a numpy array (memory is copied, not shared) a_uqtk = numpy2uqtk(a_np) print( "\nflags for original numpy array to make sure it hasn't changed to F continguous after converting" ) # verify that the original numpy array is only C contiguous assert a_np.flags['F_CONTIGUOUS'] == False assert a_np.flags['C_CONTIGUOUS'] == True print("\nConvert uqtk array back to numpy array and make sure C contiguous") b_np = uqtk2numpy(a_uqtk) # test to make sure new numpy array is *only* C contiguous (row - major) assert b_np.flags['F_CONTIGUOUS'] == False assert b_np.flags['C_CONTIGUOUS'] == True # test for the dot product print( "\ncompute dot product which should be [2,1.1,6] (Note that if F contigous, the dot product would be [.1,3,6]:" ) dp = np.dot(b_np, np.ones(2)) assert np.alltrue(dp == np.array([2., 1.1, 6.]))
def __init__(self,ndim,pcorder,pctype): ''' Construction has the following inputs: ndim : (int) number of input dimensions (features) pcorder : (int) the initial order of the polynomial (changes in the algorithm) pctype : ('LU','HG') type of polynomial basis functions, e.g., Legendre, Hermite ''' self.ndim = ndim # int self.pcorder = pcorder # int self.pctype = pctype # 'LU', 'HG' # generate multi index self.__mindex_uqtk = uqtkarray.intArray2D() uqtktools.computeMultiIndex(self.ndim,self.pcorder,self.__mindex_uqtk); self.mindex = uqtk2numpy(self.__mindex_uqtk) self.__mindex0_uqtk = self.__mindex_uqtk # keep original # get projection/ Vandermonde matrix self.__Phi_uqtk = uqtkarray.dblArray2D() # check if compiled self.__compiled = False self.compile() self.__cv_flag = False def compile(self,l_init=0.0,adaptive=0,optimal=1,scale=.1,verbose=0): ''' Setting up variables for the BCS algorithm. Most of the variables do not need to be set. Default settings are sufficient for more cases. See the C++ code for more information about variables. '''
# set 2d array to numpy array # make sure to pass asfortranarray y_np = random.randn(m,n) y.setnpdblArray(asfortranarray(y_np)) for i in range(m): for j in range(n): assert y[i,j] == y_np[i,j] ''' alternative using uqtk2numpy and numpy2uqtk ''' # test conversion from 1d numpy array to 1d uqtk array nn = 10 x1 = random.rand(nn) y1 = numpy2uqtk(x1) z1 = uqtk2numpy(y1) for i in range(nn): assert x1[i] == y1[i] # test conversion from 1d uqtk array to numpy for i in range(nn): assert z1[i] == x1[i] # test for conversion from 2d numpy to 2d uqtk nn = 10 mm = 5 X1 = random.rand(mm,nn) Y1 = numpy2uqtk(X1) Z1 = uqtk2numpy(Y1) for i in range(mm): for j in range(nn):