コード例 #1
0
ファイル: direct.c プロジェクト: hrkalona/CircuitSimulation
void solve_direct_methods()
{
	unsigned int dimension;
#if DEBUG
	int k;
#endif
	double *vector_temp = NULL;

	//printf("\n\nSolving (Forward/Backward) ...\n\n");
	
	dimension = circuit_simulation.number_of_nodes + circuit_simulation.group2_elements;

	if (circuit_simulation.matrix_sparsity == SPARSE)
	{
		vector_temp = ( double * ) calloc( dimension, sizeof(double) );

		if (vector_temp == NULL)
		{
			printf( "Could not allocate matrices.\n" );
			printf( "Terminating.\n" );
			exit( -1 );
		}

		memcpy( vector_x, vector_b, dimension * sizeof(double) );

		if (circuit_simulation.matrix_type == NONSPD)
		{ /*Forward Backward LU*/
			cs_di_ipvec( N->pinv, vector_x, vector_temp, dimension );
			cs_di_lsolve( N->L, vector_temp );
			cs_di_usolve( N->U, vector_temp );
			cs_di_ipvec( S->q, vector_temp, vector_x, dimension );
		}
		else
		{/*Forward Backward Cholesky*/
			cs_di_ipvec( S->pinv, vector_x, vector_temp, dimension );
			cs_di_lsolve( N->L, vector_temp );
			cs_di_ltsolve( N->L, vector_temp );
			cs_di_pvec( S->pinv, vector_temp, vector_x, dimension );
		}

		free( vector_temp );
	}
	else
	{
		forward_substitution();
		backward_substitution();
	}
	
	#if DEBUG
		printf("\nVector x:\n\n");
		for(k = 0; k < dimension; k++)
		{
			printf("%12lf\n", vector_x[k]);
		}

		printf("\n\n");
        #endif

}
コード例 #2
0
vec ls_solve(const mat &L, int p, const mat &U, int q, const vec &b)
{
    vec x(L.rows());
  
    forward_substitution(L, p, b, x); // Solve Ly=b, Here y=x
    backward_substitution(U, q, x, x); // Solve Ux=y, Here x=y
  
    return x;
}
コード例 #3
0
// simultaneous linear equation 
void solute_SLE_with_n_variables(double coefficient_matrix[N][N], double right_hand_vector[N], double solution_vector[N]){
    double l_matrix[N][N] = {0};
    double u_matrix[N][N] = {0};
    double mid_solution_vector[N];

    lu_decomposition(coefficient_matrix, l_matrix, u_matrix);
    forward_substitution(l_matrix, right_hand_vector, mid_solution_vector);
    backward_substitution(u_matrix, mid_solution_vector, solution_vector);
}
コード例 #4
0
void LU_solve(const DenseMatrix &A, const DenseMatrix &b, DenseMatrix &x)
{
    DenseMatrix L = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix U = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix x_ = DenseMatrix(b.nrows(), b.ncols());

    LU(A, L, U);
    forward_substitution(L, b, x_);
    back_substitution(U, x_, x);
}
コード例 #5
0
void fraction_free_LU_solve(const DenseMatrix &A, const DenseMatrix &b,
                            DenseMatrix &x)
{
    DenseMatrix LU = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix x_ = DenseMatrix(b.nrows(), b.ncols());

    fraction_free_LU(A, LU);
    forward_substitution(LU, b, x_);
    back_substitution(LU, x_, x);
}
コード例 #6
0
vec forward_substitution(const mat &L, int p, const vec &b)
{
 void forward_substitution(const mat &L, int p, const vec &b, vec &x);
   int n = L.rows();
    vec x(n);
  
    forward_substitution(L, p, b, x);

    return x;
}
コード例 #7
0
void pivoted_LU_solve(const DenseMatrix &A, const DenseMatrix &b,
                      DenseMatrix &x)
{
    DenseMatrix L = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix U = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix x_ = DenseMatrix(b);
    permutelist pl;

    pivoted_LU(A, L, U, pl);
    permuteFwd(x_, pl);
    forward_substitution(L, x_, x_);
    back_substitution(U, x_, x);
}
コード例 #8
0
void LDL_solve(const DenseMatrix &A, const DenseMatrix &b, DenseMatrix &x)
{
    DenseMatrix L = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix D = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix x_ = DenseMatrix(b.nrows(), b.ncols());

    if (not is_symmetric_dense(A))
        throw SymEngineException("Matrix must be symmetric");

    LDL(A, L, D);
    forward_substitution(L, b, x);
    diagonal_solve(D, x, x_);
    transpose_dense(L, D);
    back_substitution(D, x_, x);
}
コード例 #9
0
ファイル: dense_matrix.cpp プロジェクト: nitish-mittal/csympy
void LDL_solve(const DenseMatrix &A, const DenseMatrix &b, DenseMatrix &x)
{
    DenseMatrix L = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix D = DenseMatrix(A.nrows(), A.ncols());
    DenseMatrix x_ = DenseMatrix(b.nrows(), 1);

    if (!is_symmetric_dense(A))
        throw std::runtime_error("Matrix must be symmetric");

    LDL(A, L, D);
    forward_substitution(L, b, x);
    diagonal_solve(D, x, x_);
    transpose_dense(L, D);
    back_substitution(D, x_, x);
}
コード例 #10
0
void inverse_LU(const DenseMatrix &A, DenseMatrix &B)
{
    SYMENGINE_ASSERT(A.row_ == A.col_ and B.row_ == B.col_
                     and B.row_ == A.row_);

    unsigned n = A.row_, i;
    DenseMatrix L = DenseMatrix(n, n);
    DenseMatrix U = DenseMatrix(n, n);
    DenseMatrix e = DenseMatrix(n, 1);
    DenseMatrix x = DenseMatrix(n, 1);
    DenseMatrix x_ = DenseMatrix(n, 1);

    // Initialize matrices
    for (i = 0; i < n * n; i++) {
        L.m_[i] = zero;
        U.m_[i] = zero;
        B.m_[i] = zero;
    }

    for (i = 0; i < n; i++) {
        e.m_[i] = zero;
        x.m_[i] = zero;
        x_.m_[i] = zero;
    }

    LU(A, L, U);

    // We solve AX_{i} = e_{i} for i = 1, 2, .. n and combine the column vectors
    // X_{1}, X_{2}, ... X_{n} to form the inverse of A. Here, e_{i}'s are the
    // elements of the standard basis.
    for (unsigned j = 0; j < n; j++) {
        e.m_[j] = one;

        forward_substitution(L, e, x_);
        back_substitution(U, x_, x);

        for (i = 0; i < n; i++)
            B.m_[i * n + j] = x.m_[i];

        e.m_[j] = zero;
    }
}
コード例 #11
0
    int
    lu_solver( const matrix<T1,D1,A1>&           A, 
               matrix<T2,D2,A2>&                 x, 
               const matrix<T3,D3,A3>&           b )
    {
        typedef matrix<T1,D1,A1>                 matrix_type;
        //typedef typename matrix_type::value_type value_type;
        typedef typename matrix_type::size_type  size_type;

        assert( A.row() == A.col() );
        assert( A.row() == b.row() );
        assert( b.col() == 1 );
        size_type const n = A.row();

        matrix_type L, U;
        // if lu decomposition failed, return 
        if ( lu_decomposition( A, L, U ) ) return 1;

        matrix_type Y;
        if( forward_substitution( L, Y, b ) ) return 1; // solve LY=b
        if( backward_substitution( U, x, Y )) return 1; //solve Ux=Y

        return 0;
    }
/*! The type of preconditioner is asked by the user at the runtime*/
void Iterative_Solver::BiCG_preconditioner(SparseMatrix & spm, Vector<double> & X, Vector <double> & B, int choice){
	int number_of_iterations = 0;
//	cout << "Reached Here" << endl;
	Vector<double> Q;
	Q.set_size(X.get_size());
	Q.initialize();
	spm.multiply(X,Q);
	//	Q = spm*X;
	Vector<double> r;
	r.set_size(X.get_size());
	r.sub(B,Q);
	//	r = B - Q;
	Vector<double> r_star;
	r_star.set_size(X.get_size());
	r_star.copy_values(r);
	double beeta;
	double alpha = 1.0;
	double rho = 1.0;
	double omega = 1.0;
	//	double p1 = 0.0;
	double error = 199;
	Vector<double> temp;
	temp.set_size(X.get_size());
	temp.initialize();
	Vector<double> p1;
	p1.set_size(X.get_size());
	p1.initialize();
	Vector<double> v;
	v.set_size(X.get_size());
	v.initialize();
	Vector<double> s;
	s.set_size(X.get_size());
	s.initialize();
	Vector<double> t;
	t.set_size(X.get_size());
	t.initialize();
	Vector<double> temp1;
	temp1.set_size(X.get_size());
	temp1.initialize();
	SparseMatrix M;
//	cout << "Reached Here" << endl;
	Vector<double> y;
	y.set_size(X.get_size());
	Vector<double> z;
	z.set_size(X.get_size());
	if (choice == 0){
		M.initialize(X.get_size());
		M.extractdiagonal(spm);
	}
	else if (choice == 1){
		M.copy(spm);
		ILUDecomposition(M);

	}
	//	cout << "Line 145" << endl;
//	cout << "Iterations started" << endl;
	while(error > pow(10.0,-6)){
		temp.copy_values(X);
		//		cout << "Line 148" << endl;
		double rho_1 = r_star*r;
		//		cout << "Line 150" <<endl;
		beeta = (rho_1/rho)*(alpha/omega);
		//		cout << "Line 151" << endl;
		temp1.add(p1,v,-omega);
		//		p1 = r + (p1 - v*omega)*beeta;
		p1.add(r,temp1,beeta);
		if (choice == 0){
			//			y.multiply_dot(M,p1);
			M.multiply(p1,y);
			//			y.display();
		}

		else if(choice == 1){
			Vector<double> temp_y;
			temp_y.set_size(X.get_size());
			temp_y.initialize();
			forward_substitution(M,temp_y,p1);
			backward_substitution(M,y,temp_y);

		}
		else if(choice == 2){
			forward_substitution_Gauss_Siedel(spm,y,p1);
		}
		//		y.display();
		//		cout << "Line 153";
		spm.multiply(y,v);
		//		v = spm*p1;
		//		cout << "*******" << endl;
		//		v.display();
		alpha = rho_1/(r_star*v);
		//		cout << "Line 159";
		s.add(r,v,-alpha);
		//		cout << "Line 161";
		//		s = r - v*alpha;
		if(choice == 0){
			M.multiply(s,z);
			//			z.multiply_dot(M,s);

		}
		else if (choice == 1){
			Vector<double> temp_z;
			temp_z.set_size(X.get_size());
			temp_z.initialize();
			forward_substitution(M,temp_z,s);
			backward_substitution(M,z,temp_z);
		}
		else if(choice == 2){
			forward_substitution_Gauss_Siedel(spm,z,s);
		}
		spm.multiply(z,t);
		//		t.display();
		//		cout << "Line 164";
		//		t = spm*s;
		double tempval1 = t*s;
		double tempval2 = t*t;
		//		t.display();
		//		cout << tempval1 << endl;
		//		cout << tempval2 << endl;


		omega = (tempval1)/(tempval2);

		//		cout << "Line 167" << endl;
		//		temp1.initialize();
		temp1.add(X,y,alpha);
		X.add(temp1,z,omega);
		//		temp1.initialize();
		//		X = X + s*omega + p1*alpha;
		r.add(s,t,-omega);
		//		r = s - t*omega;
		rho = rho_1;
		//		cout << "Line 189" <<endl;
		error = cal_error(X,temp);
		number_of_iterations++;
	}
	cout << "number of iterations in BiCG" << number_of_iterations << endl;
//	M.del();
//	cout << "Value from Itsolver" << endl;
//	B.display();
}
コード例 #13
0
int main(){
    int kadai;

    // 課題番号指定
    while(1){
        printf("課題番号: ");
        scanf("%d", &kadai);
        if (kadai < 1 || kadai > 8 || kadai == 7){
            printf("1~8!!\n");
        } else {
            break;
        }
    }

    if (kadai == 1){

        double coefficient_mat[N][N] = {{1, 0, 0}, {3, 1, 0}, {-2, 2, 1}};
        double right_hand_vec[N] = {2, 3, -1};
        double solution_vec[N];

        printf("--------------- L行列 ---------------\n");
        print_array(coefficient_mat);
        printf("------------- 右辺ベクトル ------------\n");
        print_vector(right_hand_vec);

        forward_substitution(coefficient_mat, right_hand_vec, solution_vec);

        printf("------------- 解ベクトル --------------\n");
        print_vector(solution_vec);

    } 
    else if (kadai == 2)
    {

        double coefficient_mat[N][N] = {{2, 1, -1}, {0, 3, 2}, {0, 0, -3}};
        double right_hand_vec[N] = {2, -3, 9};
        double solution_vec[N];

        printf("--------------- U行列 ----------------\n");
        print_array(coefficient_mat);
        printf("------------- 右辺ベクトル -------------\n");
        print_vector(right_hand_vec);

        backward_substitution(coefficient_mat, right_hand_vec, solution_vec);

        printf("-------------- 解ベクトル -------------\n");
        print_vector(solution_vec);

    }
    else if (kadai == 3)
    {

        double coefficient_mat[N][N];
        double l_matrix[N][N] = {{1, 0, 0}, {3, 1, 0}, {-2, 2, 1}};
        double u_matrix[N][N] = {{2, 1, -1}, {0, 3, 2}, {0, 0, -3}};

        mat_mlt(l_matrix, u_matrix, coefficient_mat);

        printf("---------- 係数行列 ----------\n");
        print_array(coefficient_mat);

    }
    else if (kadai == 4)
    {

        double coefficient_mat[N][N] = {{2, 1, -1}, {6, 6, -1}, {-4, 4, 3}};
        double l_matrix[N][N] = {0};
        double u_matrix[N][N] = {0};

        lu_decomposition(coefficient_mat, l_matrix, u_matrix);

        printf("---------- L行列 ----------\n");
        print_array(l_matrix);
        printf("---------- U行列 ----------\n");
        print_array(u_matrix);

    }
    else if (kadai == 5)
    {

        double coefficient_mat[N][N] = {{2, 1, -1}, {6, 6, -1}, {-4, 4, 3}};
        double right_hand_vec[N] = {2, 3, -1};
        double solution_vec[N];

        printf("---------- 係数行列 ----------\n");
        print_array(coefficient_mat);
        printf("---------- 右辺ベクトル ----------\n");
        print_vector(right_hand_vec);

        solute_SLE_with_n_variables(coefficient_mat, right_hand_vec, solution_vec);

        printf("---------- 解ベクトル ----------\n");
        print_vector(solution_vec);

    }
    else if (kadai == 6)
    {

        double h_coefficient_mat[N][N];
        double right_hand_vec[N];
        double solution_vec[N];

        int i, j;
        for (i = 0; i < N; i++){
            for (j = 0; j < N; j++){
                h_coefficient_mat[i][j] = pow(0.5, abs(i-j));
            }
        }
        printf("--------------- 行列H ---------------\n");
        print_array(h_coefficient_mat);

        for (i = 0; i < N; i++){
            right_hand_vec[i] = 3 - pow(2, i-N+1) - pow(2, -i);
        }
        printf("------------- 右辺ベクトル -------------\n");
        print_vector(right_hand_vec);

        solute_SLE_with_n_variables(h_coefficient_mat, right_hand_vec, solution_vec);

        printf("---------- 解ベクトル ----------\n");
        print_vector(solution_vec);

    }
    else if (kadai == 8)
    {

        double h_mat[N][N];
        double h_reverse_mat[N][N] = {0};
        double solution_mat[N][N];

        int i, j;
        for (i = 0; i < N; i++){
            for (j = 0; j < N; j++){
                h_mat[i][j] = pow(0.5, abs(i-j));
            }
        }

        evaluate_reverse_matrix(h_mat, h_reverse_mat);

        mat_mlt(h_mat, h_reverse_mat, solution_mat);
        printf("--------------- 行列H ---------------\n");
        print_array(h_mat);
        printf("------------ 行列Hの逆行列 ------------\n");
        print_array(h_reverse_mat);
        printf("---------- 解行列 ----------\n");
        print_array(solution_mat);
    } 

    return 0;
}
コード例 #14
0
ファイル: lars.c プロジェクト: maggotroot/ETHproject
/*
 * LARS algorithm.
 *
 *
 * Suppose we expect a response variable to be determined by a linear
 * combination of a subset of potential covariates. Then the LARS
 * algorithm provides a means of producing an estimate of which
 * variables to include, as well as their coefficients.
 *
 * Instead of giving a vector result, the LARS solution consists of a
 * curve denoting the solution for each value of the L1 norm of the
 * parameter vector. The algorithm is similar to forward stepwise
 * regression, but instead of including variables at each step, the
 * estimated parameters are increased in a direction equiangular to
 * each one's correlations with the residual.
 *
 *
 * Parameters
 * ----------
 * predictors are expected to be normalized
 *
 *  X is (features, nsamples), b is (nsamples,1)
 *
 * beta is the returned array with the parameters of the regression
 * problem.
 *
 * b will be modified to store the current residual.
 *
 * TODO
 * ----
 * The case when two vectors have equal correlation.
 *
 */
void lars_fit(double *X, double *b, double *beta, int *ind, double *L, int nfeatures, int nsamples, int stop)
{
    /* temp variables. way too much */
    double *dir = (double *) calloc(nsamples, sizeof(double));
    double *dd  = (double *) malloc(nfeatures * sizeof(double));
    double *w   = (double *) malloc(nfeatures * sizeof(double));
    double *v   = (double *) malloc(nsamples * sizeof(double));
    /* TODO: we could use v and w as the same */

    double gamma = 0, tgamma, temp, cov_max, Aa = 0;
    int i, j, k, sum_k=0, sign;

    struct dllist *unused, *used_indices, *cur;
    struct dllist *pmax; /* maximum covariance (current working variable) */

    unused = (struct dllist *) malloc((nfeatures+2) * sizeof(struct dllist));

    /* if stop == nfeatures: raise Exception */

    /* create index table as a linked list */
    for (i=0; i<=nfeatures; ++i) {
        unused[i].index = i-1;
        unused[i].next = unused + i + 1;
        unused[i].prev = unused + i - 1;
    }
    cur = unused + nfeatures;
    used_indices = cur->next;

    /* set sentinels */
    used_indices->prev = NULL;
    cur->next = NULL;

    /* main loop, we iterate over the user-suplied number of iterations */
    for (k=0; k < stop; ++k) {

        /* Update residual */
        for (i=0, cur=used_indices; cur;  cur=cur->prev, ++i) {
            temp = 0;
            for (j = 0; j<k; ++j)
                temp += X[i*nsamples + ind[j]] * beta[sum_k + j];
            b[ind[i]] -= temp;
        }

        cov_max = 0;
        /* calculate covariances (c_hat), and get maximum covariance (C_hat)*/
        for (cur = unused->next; cur; cur = cur->next) {
            temp = cblas_ddot (nsamples, X + cur->index, nfeatures, b, 1);
            cur->cov = temp;
            if (fabs(temp) > cov_max) {
                sign = copysign (1.0, temp);
                cov_max = fabs (temp);
                pmax = cur;
            }
        }

        /* add pmax to the active set */
        pmax->prev->next = pmax->next;
        if (pmax->next) pmax->next->prev = pmax->prev;
        pmax->prev = used_indices;
        used_indices = pmax;

        /*
         * We compute some intermediate results that we will need to
         * compute the least-squares direction.
         */
        for (i=k, cur=used_indices; cur; cur=cur->prev, --i) {
            /*
             * To update the cholesky decomposition, we need to calculate
             *
             *                    v  = Xa' * cur
             *                    dd = Xa' * res
             *
             * where res is the current residual and cur is the last
             * vector to enter the active set.
             */
            v[i] = sign * cblas_ddot(nsamples, X + cur->index, nfeatures, X + pmax->index, nfeatures);
            dd[i] = cur->cov;
        }

        /*
         * Compute least squares solution by the method of normal equations
         * (Golub & Van Loan, 1996)
         *
         * Update L:
         *          ( L   w )
         *   L  ->  (       )  ,  where w = (L^-1) v
         *          ( 0  Ln )
         *
         * And solve ...
         *
         * sum_k is the number such that L[sum_k] is the first
         * unwritten field in L.
         *
         */
        sum_k = k * (k+1) / 2;
        back_substitution (L, v, L + sum_k, k);
        L[sum_k + k] = sqrt (cblas_dnrm2(nsamples, X + pmax->index, nfeatures)  - \
                             cblas_dnrm2(k, L + sum_k, 1));
        forward_substitution (L, dd, w, k + 1);
        back_substitution (L, w, dir, k + 1); /* dir is now the least squares direction */

        /*
         * Compute gamma.
         * We iterate over unused indices.
         */
        Aa = pmax->cov;
        gamma = INFINITY;
        for (cur = unused->next; cur; cur = cur->next) {
            temp = cblas_ddot(nsamples, X + cur->index, nfeatures, b, 1);
            tgamma = fmin_pos (cov_max - cur->cov / (Aa - temp),
                               cov_max + cur->cov / (Aa + temp));
            gamma = fmin (tgamma, gamma);
        }

        /* Set up return values */
        ind[k] = pmax->index;
        for(i=0; i<k; ++i) {
            beta[sum_k + i] = beta[sum_k - k + i] + gamma * dir[i];
        }
        beta[sum_k + k] = gamma * dir[k];
    }

    free(v);
    free(dir);
    free(unused);
}