void main() { int a[LEN][LEN] /*= { { 1,0,0,0,0,0,0,0,0,0 }, { 0,1,0,0,0,0,0,0,0,0 }, { 0,0,1,0,0,0,0,0,0,0 }, { 0,0,0,1,0,0,0,0,0,0 }, { 0,0,0,0,1,0,0,0,0,0 }, { 0,0,0,0,0,1,0,0,0,0 }, { 0,0,0,0,0,0,1,0,0,0 }, { 0,0,0,0,0,0,0,1,0,0 }, { 0,0,0,0,0,0,0,0,1,0 }, { 0,0,0,0,0,0,0,0,0,1 } }*/; initR(a); srand((unsigned)time(NULL)); int counter = 0; while( identity_matrix(&a[LEN][LEN],LEN) != 1 && counter < 2e16+1){ counter++; // printM(a); initR(a); } printM(a); printf("counter = %d\n",counter); printf("%d\n",identity_matrix(&a[LEN][LEN],LEN)); }
// ICRS to TEME conversion void icrs_to_teme(double mjd,double a[3][3]) { int i,j; double dpsi,deps,eps,z,theta,zeta,h; double p[3][3],n[3][3],q[3][3],b[3][3]; // Precession precess(51544.5,mjd,&zeta,&z,&theta); identity_matrix(p); rotate_z(-zeta,p); rotate_y(theta,p); rotate_z(-z,p); // Nutation nutation(mjd,&dpsi,&deps,&eps); identity_matrix(n); rotate_x(eps,n); rotate_z(-dpsi,n); rotate_x(-eps-deps,n); // Equation of equinoxes identity_matrix(q); rotate_z(dpsi*cos(eps+deps),q); // Multiply matrices (left to right) matrix_multiply(q,n,b); matrix_multiply(b,p,a); return; }
void danilevski(Matrix a) { int n = a.size(); Matrix B, A(a), C = identity_matrix(n); for (int i = n - 2; i >= 0; --i) { B = identity_matrix(n); for (int j = 0; j < n; ++j) { if (j != i) { B[i][j] = -1 * A[i + 1][j] / A[i + 1][i]; } else { B[i][j] = 1 / A[i + 1][i]; } } C = C * B; A = inverse(B) * A * B; } double lambda = power_iteration(a, EPS); Matrix Y(n, Vector(1, 1)); double vl = lambda; for (int i = 1; i < n; ++i) { Y[n - i - 1][0] = vl; vl *= lambda; } Matrix X = C * Y; X = X * (1. / sqrt(norm(transpose(X) * X))); cout << "Danilevski method for eigenvalues:" << endl; cout << "Given matrix:" << endl; print(a); cout << "Frobenius matrix:" << endl; print(A); check(a, transpose(X)[0], A[0], lambda); }
/** * Compute the covvariance array for AR1 case * This is based on the parameter rho. * This is done in two steps: * 1) fist calculate the correlation matrix dCor given phi * 2) Get the mle estimate of m_sig2 given the correlation. **/ void logistic_normal::compute_covariance_matrix(const dvariable& phi) { m_V.allocate(m_y1,m_y2,m_b1,m_nB2-1,m_b1,m_nB2-1); m_V.initialize(); dvar3_array dCor(m_y1,m_y2,m_b1,m_nB2,m_b1,m_nB2); dCor.initialize(); int i,j,k,nb; for( i = m_y1; i <= m_y2; i++ ) { nb = m_nB2(i); dCor(i) = identity_matrix(m_b1,nb); // 2). Compute the vector of coefficients. dvar_vector drho(m_b1,nb); for( j = m_b1; j <= nb; j++ ) { drho(j) = pow(phi,j-m_b1+1); } // 3). Compute correlation matrix dCor for( j = m_b1; j <= nb; j++ ) { for( k = m_b1; k <= nb; k++ ) { if( j != k ) dCor(i)(j,k) = drho(m_b1+abs(j-k)); } } m_V(i) = trans(trans(dCor(i).sub(m_b1,nb-1)).sub(m_b1,nb-1)); } // compute mle estimate of sigma compute_mle_sigma(m_V); // cout<<"Got to here sigma = "<<m_sig<<endl; for( i = m_y1; i <= m_y2; i++ ) { nb = m_nB2(i); for( j = m_b1; j <= nb; j++ ) { dCor(i).rowfill(j, extract_row(dCor(i),j)*m_sig ); } for( k = m_b1; k <= nb; k++ ) { dCor(i).colfill(k, extract_column(dCor(i),k)*m_sig ); } //cout<<dCor(i)<<endl; // Kmat dmatrix I = identity_matrix(m_b1,nb-1); dmatrix tKmat(m_b1,nb,m_b1,nb-1); tKmat.sub(m_b1,nb-1) = I; tKmat(nb) = -1; dmatrix Kmat = trans(tKmat); m_V(i) = Kmat * dCor(i) * tKmat; } }
double jacobi_eigenvalue(Matrix a, double precision) { double max = precision + 1; int imax, jmax, n = a.size(), k = 0; Matrix A(a); Matrix UUU = identity_matrix(a.size()); while (true) { k++; max = 0; imax = jmax = 0; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (i != j && abs(A[i][j]) > max) { max = abs(A[i][j]); imax = i; jmax = j; } } } if (max < precision) break; double phi = 1. / 2 * atan(2 * A[imax][jmax] / (A[imax][imax] - A[jmax][jmax])); Matrix U = identity_matrix(n); U[imax][imax] = cos(phi); U[jmax][jmax] = cos(phi); U[imax][jmax] = -1 * sin(phi); U[jmax][imax] = sin(phi); UUU = UUU * U; A = transpose(U) * A * U; } Vector X(n); for (int i = 0; i < n; ++i) { X[i] = A[i][i]; } Matrix X22 = column(UUU, a.size() - 1); X22 = X22 * (1 / sqrt(norm(transpose(X22) * X22))); cout << "dsfdsdsfsd"<< endl; print(UUU); print(X22); cout << "Jacobi method for eigenvalues:" << endl; cout << "Given matrix:" << endl; print(a); cout << "Transformed matrix (" << k << " iterations):" << endl; print(A); cout << "Precision:" << endl; cout << max << endl; cout << "Eigenvalues:" << endl; print(X); cout << endl; }
/* should output: * print_matrix: * 3 1 -1 * 0 -2 0 * 5 0 0 * matrix minor: * 0 0 * 5 0 * ineff_det: -10 * eff_det: 10 * lu decomposition is not unique, output can be checked manually * invert_lower_tri_matrix: * 1.000000 0.000000 0.000000 * 0.000000 1.000000 0.000000 * -1.666667 -0.833333 1.000000 * invert_upper_tri_matrix: * 0.333333 0.166667 0.200000 * 0.000000 -0.500000 0.000000 * 0.000000 -0.000000 0.600000 * invert_matrix: * 0.000000 0.000000 0.200000 * 0.000000 -0.500000 0.000000 * -1.000000 -0.500000 0.600000 */ void test_matrix_functions() { matrix *a = identity_matrix(3); a->entries[0][0] = 3.0; a->entries[0][1] = 1.0; a->entries[0][2] = -1.0; a->entries[1][1] = -2.0; a->entries[2][0] = 5.0; a->entries[2][2] = 0.0; printf("print_matrix: \n"); print_matrix(*a); printf("matrix_minor: \n"); print_matrix(*matrix_minor(*a, 1)); printf("ineff_det: %lf\n", (double) ineff_det(*a)); printf("eff_det: %lf\n", (double) eff_det(*a)); matrix *a_cpy = copy_matrix(*a); printf("lu_decomp: \n"); matrix **pl = lu_decomp(a_cpy, (int*) NULL); print_matrix(*pl[0]); // prints p print_matrix(*pl[1]); // prints l print_matrix(*a_cpy); // prints u printf("invert_lower_tri_matrix: \n"); print_matrix(*invert_lower_tri_matrix(*pl[1])); printf("invert_upper_tri_matrix: \n"); print_matrix(*invert_upper_tri_matrix(*a_cpy)); printf("invert_matrix: \n"); print_matrix(*invert_matrix(*a)); }
/** * Verify that a matrix times the identity is itself **/ void identity_test(int n) { double *A, *B, *C; printf("identity_test n=%d............", n); /* Allocate matrices */ A = random_matrix(n, n); B = identity_matrix(n, n); C = zeros_matrix(n, n); /* C = 1.0*(A*B) + 0.0*C */ local_mm(n, n, n, 1.0, A, n, B, n, 5.0, C, n); /* Verfiy the results */ verify_matrix(n, n, A, C); /* Backwards C = 1.0*(B*A) + 0.0*C */ local_mm(n, n, n, 1.0, B, n, A, n, 0.0, C, n); /* Verfiy the results */ verify_matrix(n, n, A, C); /* deallocate memory */ deallocate_matrix(A); deallocate_matrix(B); deallocate_matrix(C); printf("passed\n"); }
int main() { int Array[DEMENTION_OF_MATRIX][DEMENTION_OF_MATRIX] = { {1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,1,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,1,0}, {0,0,0,0,0,0,0,0,0,1}, }; if(identity_matrix(Array)) { printf("It is a identity matrix!\n\n"); } else { printf("It is not a identity matrix!\n\n"); } getchar(); return 0; }
static void form_rotation_matrix( /*******************************/ float r[4][4], vector vpn, /* View plane normal */ vector vup /* VUp vector */ ) { vector *rvects; /* 3 vectors */ int i, j; /* allocate the 3 vectors */ _new( rvects, 3 ); rvects[2] = normalize( vpn ); rvects[0] = normalize( cross_prod( vup, rvects[2] ) ); rvects[1] = cross_prod( rvects[2], rvects[0] ); identity_matrix( r ); for (i=0; i < 3; i++) { for (j=0; j < 3; j++) { r[i][j] = rvects[i].v[j]; } } /* free the 3 vectors */ _free( rvects ); }
/** * Compute covariance array (m_V) for no autocorrelation case * This just sets m_V to an identity matrix + 1. * * SJDM. Appears to be working fine if eps = 0. * If eps > 0, can have a larges m_sig2 depending on value of eps. **/ void logistic_normal::compute_covariance_matrix() { m_V.allocate(m_y1,m_y2,m_b1,m_nB2-1,m_b1,m_nB2-1); m_V.initialize(); int i,nb; for( i = m_y1; i <= m_y2; i++ ) { nb = m_nB2(i); dmatrix I = identity_matrix(m_b1,nb-1); m_V(i) = 1 + I; } // compute mle estimate of sigma compute_mle_sigma(m_V); // scale covariance matrix for( i = m_y1; i <= m_y2; i++ ) { for(int j = m_b1; j < m_nB2(i); j++ ) { m_V(i)(j,j) *= m_sig2; } } }
/** * @brief Get initial vector of new shell and oldshell crabs at equilibrium * @ingroup GMACS * @authors Steve Martell and John Levitt * @date Jan 3, 2015. * * @param[out] n vector of numbers at length in new shell condition * @param[out] o vector of numbers of old shell crabs at length * @param[in] A size transition matrix * @param[in] S diagonal matrix of length specific survival rates * @param[in] P diagonal matrix of length specific molting probabilities * @param[in] r vector of new recruits at length. * * @details * Jan 3, 2015. Working with John Levitt on analytical solution instead of the * numerical approach. Think we have a soln. * * Notation: \n * \f$n\f$ = vector of newshell crabs \n * \f$o\f$ = vector of oldshell crabs \n * \f$P\f$ = diagonal matrix of molting probabilities by size \n * \f$S\f$ = diagonal matrix of survival rates by size \n * \f$A\f$ = Size transition matrix \n * \f$r\f$ = vector of new recruits (newshell) \n * \f$I\f$ = identity matrix. \n * * * The following equations represent the dynamics of newshell \a n and oldshell crabs. * \f{align*}{ * n &= nSPA + oSPA + r \\ * o &= oS(I-P) + nS(I-P) * \f} * Objective is to solve the above equations for \f$n\f$ and \f$o\f$ repsectively. * First, lets solve the second equation for \f$o\f$: * \f{align*}{ * o &= n(I-P)S[I-(I-P)S]^{-1} * \f} * next substitute the above expression into first equation above and solve for \f$n\f$ * \f{align*}{ * n &= nPSA + n(I-P)S[I-(I-P)S]^{-1}PSA + r \\ * \mbox{let} \quad \beta& = [I-(I-P)S]^{-1}, \\ * r &= n - nPSA - n(I-P)S \beta PSA \\ * r &= n(I - PSA - (I-P)S \beta PSA) \\ * \mbox{let} \quad C& = (I - PSA - (I-P)S \beta PSA), \\ * n &= (C)^{-1} (r) * \f} * Note that \f$C\f$ must be invertable to solve for the equilibrium solution for \f$n\f$. * So the diagonal elements of \f$P\f$ and \f$S\f$ must be positive non-zero numbers. * * */ void calc_equilibrium(dvar_vector& n, dvar_vector& o, const dvar_matrix& A, const dvar_matrix& S, const dvar_matrix& P, const dvar_vector& r) { int nclass = n.indexmax(); dmatrix Id = identity_matrix(1,nclass); dvar_matrix B(1,nclass,1,nclass); dvar_matrix C(1,nclass,1,nclass); dvar_matrix D(1,nclass,1,nclass); B = inv(Id - (Id-P)*S); C = P * S * A; D = trans(Id - C - (Id-P)*S*B*C); // COUT(A); // COUT(inv(D)*r); n = solve(D,r); // newshell o = n*((Id-P)*S*B); // oldshell }
void initconstraintobject(ConstraintObject* co) { co->name = NULL; co->numPoints = 0; co->points = NULL; co->cp_array_size = CP_ARRAY_INCREMENT; co->constraintType = constraint_sphere; co->segment = 0; co->display_list = 0; co->display_list_is_stale = no; co->active = yes; co->visible = yes; co->radius.xyz[0] = 0.05; co->radius.xyz[1] = 0.1; co->radius.xyz[2] = 0.05; co->height = 0.1; co->constraintAxis = 0; co->constraintSign = 0; co->plane.a = 0.0; co->plane.b = 0.0; co->plane.c = 1.0; co->plane.d = 0.0; co->rotationAxis.xyz[0] = 1.0; co->rotationAxis.xyz[1] = 0.0; co->rotationAxis.xyz[2] = 0.0; co->rotationAngle = 0.0; co->translation.xyz[0] = 0.0; co->translation.xyz[1] = 0.0; co->translation.xyz[2] = 0.0; co->undeformed_translation.xyz[0] = 0.0; co->undeformed_translation.xyz[1] = 0.0; co->undeformed_translation.xyz[2] = 0.0; identity_matrix(co->from_local_xform); identity_matrix(co->to_local_xform); co->xforms_valid = yes; co->num_qs = 0; co->num_jnts = 0; co->qs = NULL; co->joints = NULL; }
mat_4D translation_comp(float dx, float dy, float dz) { mat_4D res = identity_matrix(); res.m[3] = dx; res.m[7] = dy; res.m[11] = dz; return res; }
mat_4D translation(vec_3D p) { mat_4D res = identity_matrix(); res.m[3] = p.x; res.m[7] = p.y; res.m[11] = p.z; return res; }
/* This function returns the inverse of a matrix using * the gauss elimination algorithm with an identity matrix * returns a 0x0 matrix if the given matrix isn't square */ struct matrix inverse_matrix(struct matrix m) { if(m.rows!=m.columns) { printf("Impossible to find inverse matrix\n"); printf("Imput matrix not square\n"); return create_matrix(0,0); } return gauss_elimination(m,identity_matrix(m.rows)); }
static void persp_trans_matrix( /*****************************/ float p[4][4], float zmin ) { identity_matrix( p ); p[2][2] = 1. / (1. + zmin); p[2][3] = - zmin / (1+zmin); p[3][2] = -1.; p[3][3] = 0.; }
mat_4D rot_y(float phi) { mat_4D res = identity_matrix(); res.m[0] = cosf(phi*M_PI/180); res.m[2] = sinf(phi*M_PI/180); res.m[8] = -sinf(phi*M_PI/180); res.m[10] = cosf(phi*M_PI/180); return res; }
mat_4D rot_x(float phi) { mat_4D res = identity_matrix(); res.m[5] = cosf(phi*M_PI/180); res.m[6] = -sinf(phi*M_PI/180); res.m[9] = sinf(phi*M_PI/180); res.m[10] = cosf(phi*M_PI/180); return res; }
/* This function checks if a matrix is orthogonal * returns -1 if it can't be checked * returns 0 if it isn't orthogonal * returns 1 if it is orthogonal */ int is_orthogonal(struct matrix m) { if(m.rows!=m.columns) { printf("This matrix isn't square\n"); return -1; } struct matrix c=traspose(m); c=matrix_multiplication(m,c); return compare_matrix(c,identity_matrix(c.rows)); }
mat_4D rot_z(float phi) { mat_4D res = identity_matrix(); res.m[0] = cosf(phi*M_PI/180); res.m[1] = -sinf(phi*M_PI/180); res.m[4] = sinf(phi*M_PI/180); res.m[5] = cosf(phi*M_PI/180); return res; }
static float * shear_xy_matrix( /*****************************/ float m[4][4], float shx, float shy ) { identity_matrix( m ); m[0][2] = shx; m[1][2] = shy; return( m ); }
/** * @ingroup GMACS * @brief Calculate equilibrium vector n given A, S and r * @details Solving a matrix equation for the equilibrium number * of crabs in length interval. * * * * @param[out] n vector of numbers at length * @param[in] A size transition matrix * @param[in] S diagonal matrix of length specific survival rates * @param[in] r vector of new recruits at length. */ void calc_equilibrium(dvar_vector& n, const dvar_matrix& A, const dvar_matrix& S, const dvar_vector r) { int nclass = n.indexmax(); dmatrix Id = identity_matrix(1,nclass); dvar_matrix At(1,nclass,1,nclass); At = trans(A*S); n = -solve(At-Id,r); }
Matrix binpow(U b) const { static_assert(std::is_integral<U>::value, "Degree must be integral. For real degree use pow."); Matrix ret = identity_matrix(rows_cnt_, cols_cnt_); Matrix a = *this; while (b != 0) { if ((b & 1) != 0) { ret *= a; } a *= a; b >>= 1; } return ret; }
void initwrapobject(dpWrapObject* wo) { wo->name = NULL; wo->wrap_type = dpWrapSphere; wo->wrap_algorithm = WE_HYBRID_ALGORITHM; wo->segment = 0; wo->display_list = 0; wo->display_list_is_stale = no; wo->active = yes; /* set wrap object fields to default values */ wo->visible = yes; wo->show_wrap_pts = no; wo->radius[0] = 0.05; wo->radius[1] = 0.1; wo->radius[2] = 0.05; wo->height = 0.1; wo->wrap_axis = 0; wo->wrap_sign = 0; wo->rotation_axis.xyz[0] = 1.0; wo->rotation_axis.xyz[1] = 0.0; wo->rotation_axis.xyz[2] = 0.0; wo->rotation_angle = 0.0; wo->translation.xyz[0] = 0.0; wo->translation.xyz[1] = 0.0; wo->translation.xyz[2] = 0.0; wo->undeformed_translation.xyz[0] = 0.0; wo->undeformed_translation.xyz[1] = 0.0; wo->undeformed_translation.xyz[2] = 0.0; identity_matrix(wo->from_local_xform); identity_matrix(wo->to_local_xform); wo->xforms_valid = yes; #if VISUAL_WRAPPING_DEBUG wo->num_debug_glyphs = 0; #endif }
static float * trans_matrix( /**************************/ float m[4][4], float dx, float dy, float dz ) { identity_matrix( m ); m[0][3] = dx; m[1][3] = dy; m[2][3] = dz; return( m ); }
static float * scale_matrix( /**************************/ float m[4][4], float sx, float sy, float sz ) { identity_matrix( m ); m[0][0] = sx; m[1][1] = sy; m[2][2] = sz; return( m ); }
void lookat_matrix(float *mat, esVec3f eye, esVec3f at, esVec3f up) { esVec3f forw = { at.x - eye.x, at.y - eye.y, at.z - eye.z, }; normalize(&forw); esVec3f side = cross(up, forw); normalize(&side); up = cross(forw, side); float m0[16]; identity_matrix(m0); m0[ 0] = side.x; m0[ 4] = side.y; m0[ 8] = side.z; m0[ 1] = up.x; m0[ 5] = up.y; m0[ 9] = up.z; m0[ 2] = -forw.x; m0[ 6] = -forw.y; m0[10] = -forw.z; float m1[16]; identity_matrix(m1); m1[12] = -eye.x; m1[13] = -eye.y; m1[14] = -eye.z; mul_matrix(mat, m1, m0); }
void identity_interface(){ /* Interface to create the identity matrix */ int size; printf("Insert how many line/columns your matrix have: "); scanf("%d", &size); bool check_matrix = verify(2, size, size, 0, 0); if(check_matrix == true){ double (**matrix) = identity_matrix(size); printf("\nThe identity matrix is:\n"); print_matrix(size, size, matrix); }else{ printf("There is an error with your input data.\nCheck them please.\n"); } }
// inverts an upper-triangular matrix by converting to a lower-triangular one and using the above algorithm // runtime: O(n^3) static matrix *invert_upper_tri_matrix(matrix a) { matrix *identity = identity_matrix(a.m); matrix *lower_tri = transpose(a); // same as before, only we do not transpose the result matrix *result = alloc_matrix(a.m, a.n); for (int i=0; i<a.m; i++) { for (int j=0; j<a.n; j++) { matrix_entry sum_previous_entries = 0; for (int k=0; k<j; k++) { sum_previous_entries += lower_tri->entries[j][k] * result->entries[i][k]; } result->entries[i][j] = (identity->entries[i][j] - sum_previous_entries) / lower_tri->entries[j][j]; } } free_matrix(identity); free_matrix(lower_tri); return result; }
// inverts a lower-triangular matrix by forward substitution // runtime: O(n^3) static matrix *invert_lower_tri_matrix(matrix a) { matrix *identity = identity_matrix(a.m); // first we calculate the transpose of the result, as this is easier matrix *result_transposed = alloc_matrix(a.m, a.n); for (int i=0; i<a.m; i++) { for (int j=0; j<a.n; j++) { matrix_entry sum_previous_entries = 0; for (int k=0; k<j; k++) { sum_previous_entries += a.entries[j][k] * result_transposed->entries[i][k]; } result_transposed->entries[i][j] = (identity->entries[i][j] - sum_previous_entries) / a.entries[j][j]; } } free_matrix(identity); matrix *result = transpose(*result_transposed); free_matrix(result_transposed); return result; }