示例#1
0
文件: utils.py 项目: eabram/pointLISA
def velocity_calc(OBJ,i,time,hstep,side,rs): #used
    #LA=la()

    [i_OBJ,i_next] = i_slr(i,side=side)
    v_pos_l = OBJ.v_l_stat_func_tot(i_OBJ,time)
    v_pos_r = OBJ.v_r_stat_func_tot(i_OBJ,time)
    n = np.cross(v_pos_r,v_pos_l)
    n = LA.unit(n)
    if side=='l':
        v_pos = v_pos_l
    elif side=='r':
        v_pos = v_pos_r 

    pos_OBJ = np.array(OBJ.putp(i_OBJ,time))
    pos_next = np.array(OBJ.putp(i_next,time))
    pos_OBJ_h = np.array(OBJ.putp(i_OBJ,time+np.float64(hstep)))
    pos_next_h = np.array(OBJ.putp(i_next,time+np.float64(hstep)))
    v = ((pos_next_h-pos_next) - (pos_OBJ_h - pos_OBJ))/hstep

    v_out = n*(np.dot(v,n))
    v_arm = v*(np.dot(LA.unit(v),LA.unit(v_pos)))
    v_in = v - v_out - v_arm

    v_out_sign = np.sign(np.dot(v_out,n))
    v_arm_sign = np.sign(np.dot(LA.unit(v),LA.unit(v_pos)))
    v_in_sign = np.sign(np.dot(v_in,v_pos_r - v_pos_l))
    
    v_out_mag = np.linalg.norm(v_out)*v_out_sign
    v_in_mag = np.linalg.norm(v_in)*v_in_sign
    v_arm_mag = np.linalg.norm(v_arm)*v_arm_sign
    
    ret =  [v,v_in,v_out,v_arm,v_in_mag,v_out_mag,v_arm_mag]

    return ret[rs]
示例#2
0
def inv(M):
    # check dimension of M
    m, n = M.shape
    if (m != n):
        print('It is not a square matrix, no inverse define !')
    else:
        augmentedM = np.zeros((n, 2 * n), float)
        augmentedM[:, :n] = M[:, :]
        for i in range(n):
            augmentedM[i, n + i] = 1

        # reduced row echelon form
        R, rank, nre = LA.rref(augmentedM)
        # check if M is full rank or not
        fullRank = 1
        for i in range(n):
            if (R[i, i] == 0):
                fullRank = 0
                break

        if (fullRank == 0):
            print('The matrix is singular. Try pseudo-inverse!')
        else:
            inverse = np.array(R[:, n:])
            return inverse
示例#3
0
def solve(A, b):
    m, n = A.shape

    # augmentedA = [A|b]
    augmentedA = np.zeros((m, n + 1), float)
    augmentedA[:, :n] = A
    augmentedA[:, n] = b
    R, rankAb, nre = LA.rref(augmentedA)

    # compute the rank of A
    rankA = 0
    i = 0
    j = 0
    while (i < m and j < n):
        pivot = R[i, j]
        if (pivot == 0):
            j += 1
        else:
            rankA += 1
            i += 1
            j += 1
    print(rankA)
    print(rankAb)
    # rankA == rankAb == n : only solution
    # rankA == rankAb < n  : infinite solutions
    # rankAb > rankA : no solution
    if (rankA == rankAb):
        if (rankA == n):
            x = np.array(R[:n, n])
            return x
        else:  # rankA<n
            print('infinite solutions')
    else:  # rankAb>rankA
        print('no solution')
示例#4
0
def main():
    # Read the data from file
    file = sys.argv[1]  # Note file needs to have the complete path to file
    g = nx.Graph()
    with open(file) as f:
        next(f)
        for line in f:
            line = line.split()
            g.add_edge(int(line[0]), int(line[1]))
    # Run the first part of algorithm
    clusters = LA(g)
    final_clusters = []
    # Run the second part of algorithm
    for cluster in clusters:
        final_clusters.append(IS2(cluster, g))
    # Remove duplicate cluster
    final_without_duplicates = []
    for fc in final_clusters:
        fc = sorted(fc)
        if fc not in final_without_duplicates:
            final_without_duplicates.append(fc)
    # Write to file
    with open("./result/output.txt", 'w') as f:
        for fwd in final_without_duplicates:
            line = " ".join(map(str, fwd))
            f.write(line + '\n')
示例#5
0
文件: LA.py 项目: ntudavid/LA_lab
def solve(A,b):
    m,n = A.shape

    # augmentedA = [A|b]
    augmentedA = np.zeros((m,n+1),float)
    augmentedA[:,:n] = A
    augmentedA[:,n] = b
    R, rankAb, nre = LA.rref(augmentedA)

    # compute the rank of A
    rankA = 0
    i = 0
    j = 0
    while (i<m and j<n):
        pivot = R[i,j]
        if(pivot==0):
            j += 1
        else:
            rankA += 1
            i += 1
            j += 1
    print(rankA)
    print(rankAb)
    # rankA == rankAb == n : only solution
    # rankA == rankAb < n  : infinite solutions
    # rankAb > rankA : no solution
    if (rankA == rankAb):
        if (rankA==n):
            x=np.array(R[:n,n])
            return x
        else: # rankA<n
            print('infinite solutions')
    else: # rankAb>rankA
        print('no solution')
示例#6
0
文件: LA.py 项目: ntudavid/LA_lab
def inv(M):
    # check dimension of M
    m,n = M.shape
    if (m!=n):
        print('It is not a square matrix, no inverse define !')
    else :
        augmentedM = np.zeros((n,2*n),float)
        augmentedM[:,:n] = M[:,:]
        for i in range(n):
            augmentedM[i,n+i]=1

        # reduced row echelon form               
        R, rank, nre = LA.rref(augmentedM)
        # check if M is full rank or not
        fullRank=1;
        for i in range(n):
            if(R[i,i]==0):
                fullRank=0
                break;
        
        if (fullRank==0):
            print('The matrix is singular. Try pseudo-inverse!')
        else:
            inverse = np.array(R[:,n:])
            return inverse
示例#7
0
文件: utils.py 项目: eabram/pointLISA
def calc_PAA_lout(OBJ,i,t):
    #LA = la()
    #if OBJ.abb:
    #    v_abb = [OBJ.v_in_l(i,t),OBJ.v_out_mag_l(i,t),OBJ.v_arm_mag_l(i,t)]
    #else:
    calc_ang=LA.ang_in_out(OBJ.v_l_func_tot(i,t),-OBJ.u_l_func_tot(i,t),OBJ.n_func(i,t),OBJ.r_func(i,t),give='out')
    return calc_ang
示例#8
0
文件: utils.py 项目: eabram/pointLISA
def calc_PAA_rtot(OBJ,i,t):
    #LA = la()
    #if OBJ.abb:
    #    v_abb = [OBJ.v_in_l(i,t),OBJ.v_out_mag_l(i,t),OBJ.v_arm_mag_l(i,t)]
    #else:
    calc_ang=LA.angle(OBJ.v_r_func_tot(i,t),-OBJ.u_r_func_tot(i,t))
    return calc_ang
示例#9
0
文件: utils.py 项目: eabram/pointLISA
def relativistic_aberrations(OBJ,i,t,tdel,side,relativistic=True): #used
    [i_self,i_left,i_right] = i_slr(i)
    if OBJ.calc_method=='Abram':
        tdel0=0
    elif OBJ.calc_method=='Waluschka':
        tdel0 = tdel

    if side=='l':
        u_not_ab = np.array(OBJ.putp(i_self,t-tdel0)) - np.array(OBJ.putp(i_left,t-tdel))
        u_ab = np.linalg.norm(u_not_ab)*(LA.unit(LA.unit(u_not_ab)*OBJ.c+(OBJ.vel.abs(i_self,t-tdel0) - OBJ.vel.abs(i_left,t-tdel))))

    elif side=='r':
        u_not_ab = np.array(OBJ.putp(i_self,t-tdel0)) - np.array(OBJ.putp(i_right,t-tdel))
        u_ab = np.linalg.norm(u_not_ab)*(LA.unit(LA.unit(u_not_ab)*OBJ.c+(OBJ.vel.abs(i_self,t-tdel0) - OBJ.vel.abs(i_right,t-tdel))))
 
    if relativistic==False:
        return u_ab
    
    elif relativistic==True:
        coor = methods.coor_SC(OBJ,i_self,t-tdel0)
        if side=='l':
            velo = (OBJ.vel.abs(i_self,t-tdel0) - OBJ.vel.abs(i_left,t-tdel))
        elif side=='r':
            velo = (OBJ.vel.abs(i_self,t-tdel0) - OBJ.vel.abs(i_right,t-tdel))

        c_vec = LA.unit(u_not_ab)*c

        r = coor[0]
        x_prime = LA.unit(velo)
        n_prime = LA.unit(np.cross(velo,r))
        r_prime = LA.unit(np.cross(n_prime,x_prime))

        coor_velo = np.array([r_prime,n_prime,x_prime])
        c_velo = LA.matmul(coor_velo,c_vec)
        v = np.linalg.norm(velo)
        den = 1.0 - ((v/(c**2))*coor_velo[2])
        num = ((1.0-((v**2)/(c**2)))**0.5)

        ux_prime = (c_velo[2] - v)/den
        ur_prime = (num*c_velo[0])/den
        un_prime = (num*c_velo[1])/den
        c_prime = ux_prime*x_prime + un_prime*n_prime +ur_prime*r_prime
        u_new=LA.unit(c_prime)*np.linalg.norm(u_not_ab)
   
    return u_new
示例#10
0
def det(M):
    # check dimension of M
    m, n = M.shape
    if (m != n):
        print('It is not a square matrix, no determinant define !')
    else:
        # U : upper triangular (row echelon matrix)
        # nre : number of row-exchange
        U, rank, nre = LA.upperTri(M)

        # the determinant of M is the product of U[i,i]
        determinant = 1
        for i in range(m):
            determinant = determinant * U[i, i]

        # number of row-exchange will decide the sign
        if (nre % 2 == 1):  # nre is odd
            determinant = -1 * determinant
        # determinant = LA.det(M)
        return determinant
示例#11
0
文件: LA.py 项目: ntudavid/LA_lab
def det(M):
    # check dimension of M
    m,n = M.shape
    if (m!=n):
        print('It is not a square matrix, no determinant define !')
    else :
        # U : upper triangular (row echelon matrix)
        # nre : number of row-exchange
        U,rank,nre = LA.upperTri(M)
        
        # the determinant of M is the product of U[i,i]
        determinant = 1;
        for i in range(m):
            determinant = determinant*U[i,i]

        # number of row-exchange will decide the sign
        if (nre%2==1): # nre is odd
            determinant = -1*determinant
        # determinant = LA.det(M)
        return determinant
示例#12
0
文件: main.py 项目: ItsRaza/PySharp
import regex
import WordSplitter
import LA
import SA
import Semantic
import SymbolTable
import ClassDataTable
import ClassTable
import ICG

TKs = LA.lexer('SETest1.txt')
print(len(TKs))
for T in TKs:
    print(T.CP)

print('\n')

# print(ICG.CreateLable())

Semantic.SA(TKs)
# ICG.SA(TKs)
示例#13
0
print('\nEdge with highest betweeness centrality is')
print(
    tuple(G._node[str(x)]['name'] for x in sorted(
        edge_freq, key=lambda x: edge_freq[x], reverse=True)[0]))

#TASK 4
#D is Degree Matrix, A is Adjacency Matrix, L is Laplacian matrix
L = [[
    degree_dist[i] if i == j else (-1 if i in adj_list[j] else 0)
    for j in sorted(vertices)
] for i in sorted(vertices)]
norm_L = [[((1 if degree_dist[i] else 0) if i == j else
            (-1 / ((degree_dist[i]) *
                   (degree_dist[j]))**0.5 if i in adj_list[j] else 0))
           for j in sorted(vertices)] for i in sorted(vertices)]
_ = LA.eigj(L)
eig = {_[0][i]: _[1].T[i] for i in range(len(_[0]))}
if (np.array(L) == np.array(L).T).all():
    print('\nObtained Eigen Values are Real')
else:
    print('\nObtained Eigen Values are Imaginary')

#TASK 5
print('\nSmallest Eigen Value is', min(eig))
print('\nEigen Vector corresponding to Smallest Eigen value is')
print(eig[min(eig)] / eig[min(eig)].min())
print('\nDifference from Ideal Eigen value is ', min(eig))
print('\nMaximum Difference Component-wise from Ideal Eigen vector is',
      (np.array([1] * len(vertices)) -
       (eig[min(eig)] / eig[min(eig)].min())).max())
_ = min(eig), eig[min(eig)]
示例#14
0
                        shape=(valUserNum, valXiNum_I))
vecPI_Xi_U = csr_matrix(np.ones((valItemNum, valXiNum_U)) / valItemNum,
                        shape=(valItemNum, valXiNum_U))
vecPI_Xi_I = csr_matrix(np.ones((valItemNum, valXiNum_I)) / valItemNum,
                        shape=(valItemNum, valXiNum_I))
vecPT_Xi_U = csr_matrix(np.ones((valTagNum, valXiNum_U)) / valTagNum,
                        shape=(valTagNum, valXiNum_U))
vecPT_Xi_I = csr_matrix(np.ones((valTagNum, valXiNum_I)) / valTagNum,
                        shape=(valTagNum, valXiNum_I))

#
# create matA
#

if len(matX_UU) != 0:
    matA_UU = LA.linear_combination(valUserNum, valUserNum, matX_UU,
                                    vecTheta_UU)
    matA_UU.data[:] = 1 / (1 + exp(-1 * matA_UU.data))
else:
    matA_UU = csr_matrix((valUserNum, valUserNum), dtype=float)

if len(matX_UI) != 0:
    matA_UI = LA.linear_combination(valUserNum, valItemNum, matX_UI,
                                    vecTheta_UI)
    matA_UI.data[:] = 1 / (1 + exp(-1 * matA_UI.data))
else:
    matA_UI = csr_matrix((valUserNum, valItemNum), dtype=float)

if len(matX_UT) != 0:
    matA_UT = LA.linear_combination(valUserNum, valTagNum, matX_UT,
                                    vecTheta_UT)
    matA_UT.data[:] = 1 / (1 + exp(-1 * matA_UT.data))
def leastSquaresFit(model, parameters, data, max_iterations=None,
                    stopping_limit = 0.005):
    """General non-linear least-squares fit using the
    X{Levenberg-Marquardt} algorithm and X{automatic differentiation}.

    @param model: the function to be fitted. It will be called
        with two parameters: the first is a tuple containing all fit
        parameters, and the second is the first element of a data point (see
        below). The return value must be a number.  Since automatic
        differentiation is used to obtain the derivatives with respect to the
        parameters, the function may only use the mathematical functions known
        to the module FirstDerivatives.
    @type model: callable

    @param parameters: a tuple of initial values for the
        fit parameters
    @type parameters: C{tuple} of numbers

    @param data: a list of data points to which the model
        is to be fitted. Each data point is a tuple of length two or
        three. Its first element specifies the independent variables
        of the model. It is passed to the model function as its first
        parameter, but not used in any other way. The second element
        of each data point tuple is the number that the return value
        of the model function is supposed to match as well as possible.
        The third element (which defaults to 1.) is the statistical
        variance of the data point, i.e. the inverse of its statistical
        weight in the fitting procedure.
    @type data: C{list}

    @returns: a list containing the optimal parameter values
        and the chi-squared value describing the quality of the fit
    @rtype: C{(list, float)}
    """
    n_param = len(parameters)
    p = ()
    i = 0
    for param in parameters:
        p = p + (DerivVar(param, i),)
        i = i + 1
    id = N.identity(n_param)
    l = 0.001
    chi_sq, alpha = _chiSquare(model, p, data)
    niter = 0
    while 1:
        delta = LA.solve_linear_equations(alpha+l*N.diagonal(alpha)*id,
                                          -0.5*N.array(chi_sq[1]))
        next_p = map(lambda a,b: a+b, p, delta)
        next_chi_sq, next_alpha = _chiSquare(model, next_p, data)
        if next_chi_sq > chi_sq:
            l = 10.*l
        else:
            l = 0.1*l
            if chi_sq[0] - next_chi_sq[0] < stopping_limit: break
            p = next_p
            chi_sq = next_chi_sq
            alpha = next_alpha
        niter = niter + 1
        if max_iterations is not None and niter == max_iterations:
            #raise IterationCountExceededError      
            print('Max iterations reach. Returning values.')
            break            
    return [p[0] for p in next_p], next_chi_sq[0]
示例#16
0
    def PAA_func(self):
        print('')
        print('Importing Orbit')
        tic = time.clock()
        Orbit = ORBIT(input_param=self.input_param)
        print(str(Orbit.linecount) + ' datapoints')
        self.orbit = Orbit
        utils.LISA_obj(self, type_select=self.LISA_opt)
        print('Done in ' + str(time.clock() - tic))
        self.SC = range(1, 4)

        self.putp_sampled = pointLISA.methods.get_putp_sampled(self)

        # Calculations
        #LA=utils.la()
        v_l_func_tot = []
        v_r_func_tot = []
        u_l_func_tot = []
        u_r_func_tot = []
        u_l0test_func_tot = []
        u_r0test_func_tot = []
        L_sl_func_tot = []
        L_sr_func_tot = []
        L_rl_func_tot = []
        L_rr_func_tot = []
        v_l_stat_func_tot = []
        v_r_stat_func_tot = []
        pos_func = []

        #--- Obtaining Velocity
        utils.velocity_func(self, hstep=self.hstep)
        utils.velocity_abs(self, hstep=self.hstep)

        for i in range(1, 4):
            [[v_l_func, v_r_func, u_l_func, u_r_func],
             [L_sl_func, L_sr_func, L_rl_func, L_rr_func],
             [u_l0_func,
              u_r0_func]] = utils.send_func(self,
                                            i,
                                            calc_method=self.calc_method)

            v_l_func_tot.append(v_l_func)
            v_r_func_tot.append(v_r_func)
            u_l_func_tot.append(u_l_func)
            u_r_func_tot.append(u_r_func)
            u_l0test_func_tot.append(u_l0_func)
            u_r0test_func_tot.append(u_r0_func)

            L_sl_func_tot.append(L_sl_func)
            L_sr_func_tot.append(L_sr_func)
            L_rl_func_tot.append(L_rl_func)
            L_rr_func_tot.append(L_rr_func)

            [i_self, i_left, i_right] = utils.i_slr(i)
            v_l_stat_func_tot.append(utils.get_armvec_func(self, i_self, 'l'))
            v_r_stat_func_tot.append(utils.get_armvec_func(self, i_self, 'r'))
            pos_func.append(utils.func_pos(self, i))

        self.v_l_func_tot = utils.func_over_sc(v_l_func_tot)
        self.v_r_func_tot = utils.func_over_sc(v_r_func_tot)
        self.u_l_func_tot = utils.func_over_sc(u_l_func_tot)
        self.u_r_func_tot = utils.func_over_sc(u_r_func_tot)
        self.u_l0test_func_tot = utils.func_over_sc(u_l0test_func_tot)
        self.u_r0test_func_tot = utils.func_over_sc(u_r0test_func_tot)

        self.L_sl_func_tot = utils.func_over_sc(L_sl_func_tot)
        self.L_sr_func_tot = utils.func_over_sc(L_sr_func_tot)
        self.L_rl_func_tot = utils.func_over_sc(L_rl_func_tot)
        self.L_rr_func_tot = utils.func_over_sc(L_rr_func_tot)

        self.v_l_stat_func_tot = utils.func_over_sc(v_l_stat_func_tot)
        self.v_r_stat_func_tot = utils.func_over_sc(v_r_stat_func_tot)
        self.n_func = lambda i, t: LA.unit(
            np.cross(self.v_l_stat_func_tot(i, t), self.v_r_stat_func_tot(
                i, t)))
        self.r_func = lambda i, t: utils.r_calc(self.v_l_stat_func_tot(
            i, t), self.v_r_stat_func_tot(i, t), i)
        self.pos_func = utils.func_over_sc(pos_func)

        self.v_l_in_func_tot = lambda i, t: LA.inplane(self.v_l_func_tot(i, t),
                                                       self.n_func(i, t))
        self.v_r_in_func_tot = lambda i, t: LA.inplane(self.v_r_func_tot(i, t),
                                                       self.n_func(i, t))
        self.u_l_in_func_tot = lambda i, t: LA.inplane(self.u_l_func_tot(i, t),
                                                       self.n_func(i, t))
        self.u_r_in_func_tot = lambda i, t: LA.inplane(self.u_r_func_tot(i, t),
                                                       self.n_func(i, t))
        self.v_l_out_func_tot = lambda i, t: LA.outplane(
            self.v_l_func_tot(i, t), self.n_func(i, t))
        self.v_r_out_func_tot = lambda i, t: LA.outplane(
            self.v_r_func_tot(i, t), self.n_func(i, t))
        self.u_l_out_func_tot = lambda i, t: LA.outplane(
            self.u_l_func_tot(i, t), self.n_func(i, t))
        self.u_r_out_func_tot = lambda i, t: LA.outplane(
            self.u_r_func_tot(i, t), self.n_func(i, t))

        #--- Obtaining PAA ---
        print('Aberration: ' + str(self.aberration))
        selections = ['l_in', 'l_out', 'r_in', 'r_out']
        PAA_func_val = {}
        PAA_func_val[
            selections[0]] = lambda i, t: utils.calc_PAA_lin(self, i, t)
        PAA_func_val[
            selections[1]] = lambda i, t: utils.calc_PAA_lout(self, i, t)
        PAA_func_val[
            selections[2]] = lambda i, t: utils.calc_PAA_rin(self, i, t)
        PAA_func_val[
            selections[3]] = lambda i, t: utils.calc_PAA_rout(self, i, t)
        PAA_func_val['l_tot'] = lambda i, t: utils.calc_PAA_ltot(self, i, t)
        PAA_func_val['r_tot'] = lambda i, t: utils.calc_PAA_rtot(self, i, t)

        self.PAA_func = PAA_func_val

        self.ang_breathing_din = lambda i, time: LA.angle(
            self.v_l_func_tot(i, time), self.v_r_func_tot(i, time))
        self.ang_breathing_in = lambda i, time: LA.angle(
            self.u_l_func_tot(i, time), self.u_r_func_tot(i, time))
        self.ang_breathing_stat = lambda i, time: LA.angle(
            self.v_l_stat_func_tot(i, time), self.v_r_stat_func_tot(i, time))

        self.ang_in_l = lambda i, t: LA.ang_in(self.v_l_func_tot(
            i, t), self.n_func(i, t), self.r_func(i, t))
        self.ang_in_r = lambda i, t: LA.ang_in(self.v_r_func_tot(
            i, t), self.n_func(i, t), self.r_func(i, t))
        self.ang_out_l = lambda i, t: LA.ang_out(self.v_l_func_tot(i, t),
                                                 self.n_func(i, t))
        self.ang_out_r = lambda i, t: LA.ang_out(self.v_r_func_tot(i, t),
                                                 self.n_func(i, t))

        return self
示例#17
0
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 15 2015

Modified on Fri Oct 15 2015

@author: david

"""

import LA
import numpy as np

#A=np.array([[1,2,1],[4,5,2],[4,8,3]])
A=np.array([[1,2],[4,5],[7,8]])
#A=np.array([[0,2,3],[0,5,6],[0,8,9]])
#A=np.array([[1,2,3],[3,5,6]])
print(A)
U=LA.upperTri(A)
R=LA.rref(A)
print(U)
print(R)
print(LA.det(A))
print(LA.inv(A))
b=np.array([3,6,9])
print(LA.solve(A,b))
示例#18
0
def BreakWord(string):
    lexeme = ''
    res = []
    i = 0
    dotcount = 0
    while (i < len(string)):
        char = string[i]
        if (char == '/' and string[i + 1] == '*'):
            char += string[i + 1]
            i += 1
        if char in cmts:
            cmtIp = char
            if i == len(string):
                break
            lexeme += char
            i += 1
            char = string[i]
            while (Closecmt not in lexeme):
                lexeme += char
                if i == len(string) - 1:
                    break
                i += 1
                char = string[i]
            if (i == len(string)):
                res.append(lexeme)
                lexeme = ''
                break
            char = string[i]
            res.append(lexeme)
            lexeme = ''
        if char in quotes:
            qouteIp = char
            if char == '\n':
                break
            lexeme += char
            i += 1
            char = string[i]
            while (char != qouteIp):
                if char == '\n':
                    break
                lexeme += char
                i += 1
                char = string[i]
            #lexeme += char
            #i += 1
            if (char == '\n'):
                res.append(lexeme)
                lexeme = ''
                # break
            # char = string[i]
            # res.append(lexeme)
            # lexeme = ''
        if char != space:
            lexeme += char
        if i == len(string) - 1:
            if char == space or char in seperators or lexeme in seperators:
                if lexeme != '':
                    res.append(lexeme)
                    lexeme = ''
            else:
                res.append(lexeme)
                lexeme = ''
        if (i + 1 < len(string)):
            nextch = string[i + 1]
            prech = string[i - 1]

            if (nextch == '=') and (char in seperators):
                lexeme += nextch
                i += 1
            if (nextch == dot):
                dotcount += 1
                i += 1
                char = string[i]
                nextch = string[i + 1]
                if (LA.isAlpha(nextch) or len(lexeme) != 1
                        or nextch in seperators):
                    res.append(lexeme)
                    lexeme = ''
                if (dotcount > 0):
                    lexeme += char
                    #i += 1
                # elif(dotcount > 1):
                #   res.append(lexeme)
                #   lexeme = ''
            if string[i + 1] == space or string[
                    i + 1] in seperators or lexeme in seperators:
                if (char == '+' or char == '-'):
                    if (prech == '=' or prech == space or prech in operators):
                        if (string[i + 1] != space):
                            i = i + 1
                            char = string[i]
                            lexeme += char
                            while (char not in seperators
                                   and string[i + 1] not in seperators
                                   and string[i + 1] != space):
                                i = i + 1
                                char = string[i]
                                lexeme += char
                if ((lexeme != '') and (dot not in lexeme)):
                    res.append(lexeme)
                    lexeme = ''
            if (nextch == ';' and lexeme):
                res.append(lexeme)
                lexeme = ''
            if char == dot and len(lexeme) == 1 and LA.isAlpha(nextch):
                res.append(lexeme)
                lexeme = ''
        i = i + 1
    return res
示例#19
0
文件: runLA.py 项目: ntudavid/LA_lab
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 15 2015

Modified on Fri Oct 15 2015

@author: david

"""

import LA
import numpy as np

# A=np.array([[1,2,1],[4,5,2],[4,8,3]])
A = np.array([[1, 2], [4, 5], [7, 8]])
# A=np.array([[0,2,3],[0,5,6],[0,8,9]])
# A=np.array([[1,2,3],[3,5,6]])
print(A)
U = LA.upperTri(A)
R = LA.rref(A)
print(U)
print(R)
print(LA.det(A))
print(LA.inv(A))
b = np.array([3, 6, 9])
print(LA.solve(A, b))
示例#20
0
if 'output_data' not in os.listdir():
    os.mkdir('output_data')

file_out = open('output_data/output_problem2.txt', 'w')

LA.streams = [sys.stdout, file_out]

print = LA.my_print

_ = sys.argv[1]
if _ == '-type=gram-schimdt':
    #TASK 3
    ip = open(sys.argv[2])
    mat = np.array([[float(x) for x in line.strip().split()]
                    for line in ip.readlines()])
    print(str(LA.GS(mat)))

else:
    test_data_file_path = _
    try:
        train_data_file_path = sys.argv[2]
    except Exception as ex:
        'mnist_train.csv should be in Current Working Directory, Consider values from last fed model'
        pass  #PP1, DOUBT ASKED ON PIAZZA
    else:
        'Take Training Set & Build Model, perform all tasks of Problem 2'
        df1 = pd.read_csv(train_data_file_path, index_col=False, header=None)
        #df1 = shuffle(df1)
        #TASK 1
        m, n = df1.shape
        train_mat = np.array(df1)
示例#21
0
#
# read training instances
#
#  user \t item \t posIns1,posIns2,... \t negIns1,negIns2,...
#
with open(str_train_instance_path, 'r',
          encoding='utf8') as file_train_instances:
    for line in file_train_instances:
        list_training_instance.append(line)

#
# create matA
#

if len(tensor_x_uu) != 0:
    mat_a_uu = LA.linear_combination(val_user_num, val_user_num, tensor_x_uu,
                                     vec_theta_uu)
    mat_a_uu.data[:] = 2 / (1 + exp(-1 * mat_a_uu.data)) - 1
else:
    mat_a_uu = csr_matrix((val_user_num, val_user_num), dtype=float)

if len(tensor_x_ui) != 0:
    mat_a_ui = LA.linear_combination(val_user_num, val_item_num, tensor_x_ui,
                                     vec_theta_ui)
    mat_a_ui.data[:] = 2 / (1 + exp(-1 * mat_a_ui.data)) - 1
else:
    mat_a_ui = csr_matrix((val_user_num, val_item_num), dtype=float)

if len(tensor_x_ut) != 0:
    mat_a_ut = LA.linear_combination(val_user_num, val_tag_num, tensor_x_ut,
                                     vec_theta_ut)
    mat_a_ut.data[:] = 2 / (1 + exp(-1 * mat_a_ut.data)) - 1
示例#22
0
valTagNum = shape(tensor_x_ut[0])[1]

#
# read training instances
#
#  user \t item \t posIns1,posIns2,... \t negIns1,negIns2,...
#
f_TestInstances = open(str_test_instance_path, 'r')
for line in f_TestInstances:
    list_test_instance.append(line)

#
# create matA
#
if len(tensor_x_uu) != 0:
    matA_UU = LA.linear_combination(valUserNum, valUserNum, tensor_x_uu,
                                    vec_theta_uu)
    matA_UU.data[:] = 1 / (1 + exp(-1 * matA_UU.data))
else:
    matA_UU = csr_matrix((valUserNum, valUserNum), dtype=float)

if len(tensor_x_ui) != 0:
    matA_UI = LA.linear_combination(valUserNum, valItemNum, tensor_x_ui,
                                    vec_theta_ui)
    matA_UI.data[:] = 1 / (1 + exp(-1 * matA_UI.data))
else:
    matA_UI = csr_matrix((valUserNum, valItemNum), dtype=float)

if len(tensor_x_ut) != 0:
    matA_UT = LA.linear_combination(valUserNum, valTagNum, tensor_x_ut,
                                    vec_theta_ut)
    matA_UT.data[:] = 1 / (1 + exp(-1 * matA_UT.data))
示例#23
0
    OP = open('output_problem1_part1.txt','w') if part=='one' else open('output_problem1_part2.txt','w')

    N,K = (4,4) if (part=='one') else ( tuple(map(int,IP.readline().strip().split())) )
    C = tuple(map(float,IP.readline().strip().split()))
    mat = []
    for i in range(N):
        mat.append( list(map(float,IP.readline().strip().split())) )
    M = tuple(map(float,IP.readline().strip().split()))
    #N,K = len(C),len(mat[0])
    for j in range(K):
        if sum(mat[i][j] for i in range(N))>1:
            res, finite = False, None
            break
    else:
        #Call to LA Module, with Constraints
        ech,inv,pivots,ops,res,finite,sol,equations = LA.Solve(mat,C,[0]*len(M),M)
        if type(sol) is Exception:
            #print('Constrained Can\'t be Satisfied')
            res, finite = False, False

    flg = False
    if res == True:
        quantity = [float(x) for x in sol]
        if all( (0<=quantity[i] and quantity[i]<=M[i]) for i in range(K) ):
            flg = True            
    if flg:
        if finite==True:
            LA.my_print('EXACTLY ONE!',out=OP)
            LA.my_print(*(round(x,3) for x in quantity),out=OP)
        elif finite==False:
            LA.my_print('MORE THAN ONE!',out=OP)
示例#24
0
def leastSquaresFit(model,
                    parameters,
                    data,
                    max_iterations=None,
                    stopping_limit=0.005):
    """General non-linear least-squares fit using the
    X{Levenberg-Marquardt} algorithm and X{automatic differentiation}.

    @param model: the function to be fitted. It will be called
        with two parameters: the first is a tuple containing all fit
        parameters, and the second is the first element of a data point (see
        below). The return value must be a number.  Since automatic
        differentiation is used to obtain the derivatives with respect to the
        parameters, the function may only use the mathematical functions known
        to the module FirstDerivatives.
    @type model: callable

    @param parameters: a tuple of initial values for the
        fit parameters
    @type parameters: C{tuple} of numbers

    @param data: a list of data points to which the model
        is to be fitted. Each data point is a tuple of length two or
        three. Its first element specifies the independent variables
        of the model. It is passed to the model function as its first
        parameter, but not used in any other way. The second element
        of each data point tuple is the number that the return value
        of the model function is supposed to match as well as possible.
        The third element (which defaults to 1.) is the statistical
        variance of the data point, i.e. the inverse of its statistical
        weight in the fitting procedure.
    @type data: C{list}

    @returns: a list containing the optimal parameter values
        and the chi-squared value describing the quality of the fit
    @rtype: C{(list, float)}
    """
    n_param = len(parameters)
    p = ()
    i = 0
    for param in parameters:
        p = p + (DerivVar(param, i), )
        i = i + 1
    id = N.identity(n_param)
    l = 0.001
    chi_sq, alpha = _chiSquare(model, p, data)
    niter = 0
    while 1:
        delta = LA.solve_linear_equations(alpha + l * N.diagonal(alpha) * id,
                                          -0.5 * N.array(chi_sq[1]))
        next_p = map(lambda a, b: a + b, p, delta)
        next_chi_sq, next_alpha = _chiSquare(model, next_p, data)
        if next_chi_sq > chi_sq:
            l = 10. * l
        else:
            l = 0.1 * l
            if chi_sq[0] - next_chi_sq[0] < stopping_limit: break
            p = next_p
            chi_sq = next_chi_sq
            alpha = next_alpha
        niter = niter + 1
        if max_iterations is not None and niter == max_iterations:
            #raise IterationCountExceededError
            print('Max iterations reach. Returning values.')
            break
    return [p[0] for p in next_p], next_chi_sq[0]