/* * Name : PPCFFRELS_initAsSteady * Description : 用稳态的参数初始化,在使用辨识之前需要先用该函数初始化,并指定3个必要的变量,注意这里辨识的初值没有让用户指定 * Entry : PPCFFRELS_T的结构体指针,nf1(不包括积分项)的阶数,ng的阶数,控制延迟d * Return : void * Author : lynx [email protected]. * * History * ---------------------- * Rev : 0.00 * Date : 06/17/2013 * * create. * ---------------------- */ void PPCFFRELS_initAsSteady(PPCFFRELS_T* relsIn, int na, int nb, int d, float am1, float am2, float am3) { int RELS_i = 0; //循环用的变量 const int MaxConstLength = 6; const float Steady_thetae_1[6][1] = {{0.00506070970223235},{0.00635502256773037},{0.00494823142213283},{0.000918467319518595},{0.491331722635412},{-0.483337742203960}}; const float Steady_P[6][6] = {{7.25801162530924e-06,-7.70027366576231e-06,1.21214865112869e-06,6.02023100038719e-07,-5.71430472170938e-06,-6.23557582535170e-06},{ -7.70027366575120e-06,1.54079156168219e-05,-9.03773372725222e-06,1.02078711838165e-06,1.97846289919320e-05,-1.69472474860069e-05},{ 1.21214865106959e-06,-9.03773372733294e-06,1.54822818505554e-05,-8.13508172041828e-06,2.26339537588858e-05,-1.80521927029820e-05},{ 6.02023100076411e-07,1.02078711837705e-06,-8.13508172005746e-06,8.24563127892771e-06,-5.87241585034854e-05,4.33848944347747e-05},{ -5.71430472058451e-06,1.97846289907165e-05,2.26339537754104e-05,-5.87241585080893e-05,0.00365387551157519,-0.00344564862994308},{ -6.23557582636281e-06,-1.69472474839917e-05,-1.80521927234402e-05,4.33848944426555e-05,-0.00344564862984164,0.00344611855273718}}; PPCFFRELS_init(relsIn, na, nb, d, am1, am2, am3); //为了减少冗余代码,先正常初始化 //RELS初始化 //按照给定的值初始化 for(RELS_i=0;RELS_i<relsIn->ML && RELS_i<MaxConstLength;RELS_i++){ relsIn->thetae_1[RELS_i][0] = Steady_thetae_1[RELS_i][0]; //把预先设定的值给 } //因为把thetae的更新提前了,这里就对一个初始化防止thetae变0 matrix_copy((float*)relsIn->thetae_1, relsIn->ML, 1, (float*)relsIn->thetae); //按照给定的值初始化 if(relsIn->ML > MaxConstLength){ matrix_copy((float*)Steady_P, MaxConstLength, MaxConstLength, (float*)relsIn->P); }else{ matrix_copy((float*)Steady_P, relsIn->ML, relsIn->ML, (float*)relsIn->P); } //根据这个计算出一个初始的控制器 PPCFFRELS_ClcFGR(relsIn); }
/* Performs [Q,R] = qr(M,'0') compact QR factorization M is mxn ; Q is mxn ; R is min(m,n) x min(m,n) */ void compact_QR_factorization(mat *M, mat *Q, mat *R){ int i,j,m,n,k; m = M->nrows; n = M->ncols; k = min(m,n); mat *R_full = matrix_new(m,n); matrix_copy(R_full,M); vec *tau = vector_new(m); // get R //LAPACKE_dgeqrf(CblasRowMajor, m, n, R_full->d, n, tau->d); culaDgeqrf(m, n, R_full->d, m, tau->d); for(i=0; i<k; i++){ for(j=0; j<k; j++){ if(j>=i){ matrix_set_element(R,i,j,matrix_get_element(R_full,i,j)); } } } // get Q matrix_copy(Q,R_full); //LAPACKE_dorgqr(CblasRowMajor, m, n, n, Q->d, n, tau->d); culaDorgqr(m, n, n, Q->d, m, tau->d); // clean up matrix_delete(R_full); vector_delete(tau); }
void array_type<T>::read( istream& is, const matrix_separators* p_sep ) { static DynMatrix<typename T::value_type> tmp; matrix_read_dim(is,tmp,m_io,p_sep); if (mp_context->select().size()==0) matrix_copy(var(),tmp); else matrix_copy(var(),SelectTuple< DynMatrix<typename T::value_type>,DynMatrix<int> >(tmp,mp_context->select())); }
void matrix_rotate_z(float *m, float rad) { float mr[4*4], mt[4*4]; matrix_set_z_rotation(mr, rad); matrix_mult4x4(mr, m, mt); matrix_copy(m, mt); }
void matrix_translate_xyz(float *m, float x, float y, float z) { float mr[4*4], mt[4*4]; matrix_set_xyz_translation(mr, x, y, z); matrix_mult4x4(m, mr, mt); matrix_copy(m, mt); }
// divG_sum = A * x = sum(divG(x)) // memory for the temporary pyramid px should be allocated static inline void multiplyA(pyramid_t* px, pyramid_t* pC, const float* const x, float* const divG_sum) { matrix_copy(pC->rows*pC->cols, x, divG_sum); // use divG_sum as a temp variable pyramid_calculate_gradient(px, divG_sum); pyramid_scale_gradient(px, pC); // scale gradients by Cx,Cy from main pyramid pyramid_calculate_divergence_sum(px, divG_sum); // calculate the sum of divergences }
static void one_test (const struct matrix *A, const struct matrix *B, int i) { struct matrix R; struct matrix P; mp_ptr tp; matrix_init (&R, A->n + B->n + 1); matrix_init (&P, A->n + B->n + 1); tp = refmpn_malloc_limbs (mpn_matrix22_mul_itch (A->n, B->n)); ref_matrix22_mul (&R, A, B, tp); matrix_copy (&P, A); mpn_matrix22_mul (P.e00, P.e01, P.e10, P.e11, A->n, B->e00, B->e01, B->e10, B->e11, B->n, tp); P.n = A->n + B->n + 1; if (!matrix_equal_p (&R, &P)) { fprintf (stderr, "ERROR in test %d\n", i); gmp_fprintf (stderr, "A = (%Nx, %Nx\n %Nx, %Nx)\n" "B = (%Nx, %Nx\n %Nx, %Nx)\n" "R = (%Nx, %Nx (expected)\n %Nx, %Nx)\n" "P = (%Nx, %Nx (incorrect)\n %Nx, %Nx)\n", A->e00, A->n, A->e01, A->n, A->e10, A->n, A->e11, A->n, B->e00, B->n, B->e01, B->n, B->e10, B->n, B->e11, B->n, R.e00, R.n, R.e01, R.n, R.e10, R.n, R.e11, R.n, P.e00, P.n, P.e01, P.n, P.e10, P.n, P.e11, P.n); abort(); } refmpn_free_limbs (tp); matrix_clear (&R); matrix_clear (&P); }
int filter2d_init(filter2d_t* q, float **taps, int ntime, int nfreq, int sztime, int szfreq) { int ret = -1; bzero(q, sizeof(filter2d_t)); if (matrix_init((void***)&q->taps, ntime, nfreq, sizeof(float))) { goto free_and_exit; } matrix_copy((void**) q->taps, (void**) taps, ntime, nfreq, sizeof(float)); q->output = vec_malloc((ntime+sztime)*(szfreq)*sizeof(cf_t)); if (!q->output) { goto free_and_exit; } bzero(q->output, (ntime+sztime)*(szfreq)*sizeof(cf_t)); q->nfreq = nfreq; q->ntime = ntime; q->szfreq = szfreq; q->sztime = sztime; ret = 0; free_and_exit: if (ret == -1) { filter2d_free(q); } return ret; }
static bool poly_approximate_1(ap_manager_t* man, pk_t* po, pk_t* pa) { bool change; pk_internal_t* pk = (pk_internal_t*)man->internal; poly_obtain_C(man,pa,NULL); if (!pa->C){ poly_set(po,pa); return false; } if (po!=pa){ po->C = matrix_copy(pa->C); } change = matrix_approximate_constraint_1(pk,po->C); if (change){ if (po==pa){ if (po->F){ matrix_free(po->F); po->F = NULL; } if (po->satC){ satmat_free(po->satC); po->satC = NULL; } if (po->satF){ satmat_free(po->satF); po->satF = NULL; } } po->status = 0; man->result.flag_exact = false; } else { poly_set_save_C(po,pa); man->result.flag_exact = true; } return change; }
void matrix_vect3_rotate ( const float in[16], const float vect[3], float t, float out[16] ) { const float x = vect[0], y = vect[1], z = vect[2]; const float sin_t = sinf( t ), cos_t = cosf( t ); float res[16]; const float matrix[16] = { cos_t + ( 1.0f - cos_t ) * x * x, ( 1.0f - cos_t ) * x * y - sin_t * z, ( 1.0f - cos_t ) * x * z + sin_t * y, 0.0f, ( 1.0f - cos_t ) * y * x + sin_t * z, cos_t + ( 1.0f - cos_t ) * y * y, ( 1.0f - cos_t ) * y * z - sin_t * x, 0.0f, ( 1.0f - cos_t ) * z * x - sin_t * y, ( 1.0f - cos_t ) * z * y + sin_t * x, cos_t + ( 1.0f - cos_t ) * z * z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; matrix_matrix_mult( in, matrix, res ); vect4_copy( &in[4 * 3], &res[4 * 3] ); matrix_copy( res, out ); }
void matrix_postmult(Gmatrix *current,Gmatrix *post_matrix) /******************************************************************************* LAST MODIFIED : 2 December 1994 DESCRIPTION : Postmultiplies current by post_matrix. ==============================================================================*/ { int i,j,k; Gmatrix temp; ENTER(matrix_postmult); matrix_copy(&temp,current); for (i=0;i<GMATRIX_SIZE;i++) { for (j=0;j<GMATRIX_SIZE;j++) { current->data[i][j] = 0.0; for (k=0;k<GMATRIX_SIZE;k++) { current->data[i][j] += temp.data[i][k]*post_matrix->data[k][j]; } } } LEAVE; } /* matrix_premult */
/* build orthonormal basis matrix Q = Y; for j=1:k vj = Q(:,j); for i=1:(j-1) vi = Q(:,i); vj = vj - project_vec(vj,vi); end vj = vj/norm(vj); Q(:,j) = vj; end */ void build_orthonormal_basis_from_mat(mat *A, mat *Q){ int m,n,i,j,ind,num_ortos=2; double vec_norm; vec *vi,*vj,*p; m = A->nrows; n = A->ncols; vi = vector_new(m); vj = vector_new(m); p = vector_new(m); matrix_copy(Q, A); for(ind=0; ind<num_ortos; ind++){ for(j=0; j<n; j++){ matrix_get_col(Q, j, vj); for(i=0; i<j; i++){ matrix_get_col(Q, i, vi); project_vector(vj, vi, p); vector_sub(vj, p); } vec_norm = vector_get2norm(vj); vector_scale(vj, 1.0/vec_norm); matrix_set_col(Q, j, vj); } } vector_delete(vi); vector_delete(vj); vector_delete(p); }
void concatenate_to_matrix(Matrix mat, Matrix dest) { //Concatenate new transform with our existing matrix. Matrix m; matrix_cat(mat, dest, m); matrix_copy(m, dest); }
void bayesian_set_game(bayesian_t *game, int i, int j, matrix_t **bimat) { game->payoffs[i][j] = matrix_copy(bimat[0]); game->payoffs[j+game->m][i] = matrix_trans(bimat[1]); matrix_free(bimat[0]); matrix_free(bimat[1]); free(bimat); }
void convert_geq_to_leq(problem* prob, work_set* virtual_vars, matrix** Fr, matrix** gr){ /* Convert >= to <= */ if (prob->inequality_count > 0){ *Fr = matrix_copy(prob->F); multiply_matrix_with_scalar(-1, *Fr); *gr = matrix_copy(prob->g); multiply_matrix_with_scalar(-1, *gr); /* Find last virtual variable */ size_t r; for (r = 1; r <= prob->inequality_count; r++){ if (compare_elements(get_value_without_check(r, 1, *gr), 0) == -1){ work_set_append(virtual_vars, r+prob->equality_count); } } } }
/* * Sample from an hll cache */ static void hll_sample_cache(double **X, double ***S, double **Q, hll_t *hll, int n) { int i; for (i = 0; i < n; i++) { int j = kdtree_NN(hll->cache.Q_kdtree, Q[i]); memcpy(X[i], hll->cache.X[j], hll->dx * sizeof(double)); matrix_copy(S[i], hll->cache.S[j], hll->dx, hll->dx); } }
void matrix_vect3_switchXY ( const float in[16], float out[16] ) { float res[16]; vect4_copy( &in[4 * 0], &res[4 * 0] ); vect4_copy( &in[4 * 1], &res[4 * 1] ); vect4_copy( &in[4 * 2], &res[4 * 2] ); vect4_copy( &in[4 * 3], &res[4 * 3] ); matrix_copy( res, out ); }
void param_state_multipartite::calculate_representation ( void ) //dicht { set_paramters(); for ( int i = 0 ; i < nr_subspaces ; ++i ) { states[i].calculate_representation(); } int curr_dim=subspaces[nr_subspaces-1]; matrix_copy ( states[nr_subspaces-1].representation,temp1,subspaces[nr_subspaces-1] ); for ( int i = nr_subspaces-2 ; i >= 0 ; --i ) { matrix_tensor_prod ( states[i].representation,subspaces[i],temp1,curr_dim ,representation,subspaces[i]*curr_dim ); curr_dim *= subspaces[i]; matrix_copy ( representation,temp1,curr_dim ); } }
PMatrix matrix_inverse(PMatrix target) { PMatrix pm, tpm; size_t r, c, k, cross; if (!matrix_check_determinant(target)) return MATRIX_ERROR; tpm = matrix_copy(target); pm = matrix_make_unit(target->row); for(r = 0; r < target->row; r++) { if ( -DETERMINANT_ERROR < tpm->matrix[r][r] && tpm->matrix[r][r] < -DETERMINANT_ERROR ) { for(k = 0; k < target->row; k++) { if (-DETERMINANT_ERROR < tpm->matrix[k][r] && tpm->matrix[k][r] < DETERMINANT_ERROR) continue; for (c = 0; c < target->column; c++) { tpm->matrix[r][c] += tpm->matrix[k][c]; pm->matrix[r][c] += pm->matrix[k][c]; } break; } if ( -DETERMINANT_ERROR < tpm->matrix[r][r] && tpm->matrix[r][r] < -DETERMINANT_ERROR ) return MATRIX_CALCULATE_ERROR; } } for(r = 0; r < target->row; r++) { cross = tpm->matrix[r][r]; for(c = 0; c < target->column; c++) { tpm->matrix[r][c] /= cross; tpm->matrix[r][c] /= cross; } for(k = 0; k < target->row; k++) { if (k == r) continue; if (tpm->matrix[k][r] == 0.0f) continue; cross = tpm->matrix[k][r]; for(c = 0; c < tpm->column; c++) { tpm->matrix[k][c] -= tpm->matrix[r][c] * cross; pm->matrix[k][c] -= pm->matrix[r][c] * cross; } } } return pm; }
void matrix_swap_xy(float *m) { float ms[4*4], mt[4*4]; matrix_identity4x4(ms); ms[0] = 0.0f; ms[1] = 1.0f; ms[4] = 1.0f; ms[5] = 0.0f; matrix_mult4x4(ms, m, mt); matrix_copy(m, mt); }
static void poly_set_save_C(pk_t* po, pk_t* pa) { if (po != pa){ po->F = pa->F ? matrix_copy(pa->F) : NULL; po->satC = pa->satC ? satmat_copy(pa->satC) : NULL; po->satF = pa->satF ? satmat_copy(pa->satF) : NULL; po->status = pa->status; po->nbeq = pa->nbeq; po->nbline = pa->nbline; po->status = pa->status; } }
void test_matrix() { struct Matrix* m = matrix_generate(5,5); printf("Matrix null: %d\n", matrix_is_null(m)); matrix_make_identity(m); struct Matrix* m2 = matrix_copy(m); matrix_set(m2,1,2,1); struct Matrix* m3 = matrix_mul(m2,m); matrix_show(m3); matrix_free(m); matrix_free(m2); matrix_free(m3); }
void matrix_scalar_multiply(Matrix *xx, double ss, Matrix *yy) { // In R notation, executes yy <- ss * xx. User responsible // for allocating memory to yy. yy can be the same as xx. const index_t nrtnc=numrows(xx)*numcols(xx); matrix_copy(xx,yy); index_t ii; for (ii=0; ii<nrtnc; ii++) yy[ii] *= ss; return; }
/* returns Q from [Q,R] = qr(M,'0') compact QR factorization M is mxn ; Q is mxn ; R is min(m,n) x min(m,n) */ void QR_factorization_getQ(mat *M, mat *Q){ int i,j,m,n,k; m = M->nrows; n = M->ncols; k = min(m,n); matrix_copy(Q,M); vec *tau = vector_new(m); culaDgeqrf(m, n, Q->d, m, tau->d); culaDorgqr(m, n, n, Q->d, m, tau->d); // clean up vector_delete(tau); }
/*! @brief Reduce Row Echelon Form */ struct Matrix* matrix_rref (struct Matrix* matrix) { struct Matrix *m_rref = NULL; double *m_wrow = NULL; size_t lead = 0; size_t rowCount, colCount; size_t r = 0, i = 0; if (!matrix) return NULL; m_rref = matrix_copy(matrix); // rowCount = matrix->nr; colCount = matrix->nc; for (r = 0; r < rowCount; r++) { if (colCount <= lead) break; i = r; while (m_rref->data[i][lead] == 0) { i++; if (rowCount == i) { i = r; lead++; if (colCount == lead) break; } } // Swap row i and r m_wrow = m_rref->data[i]; m_rref->data[i] = m_rref->data[r]; m_rref->data[r] = m_wrow; for (i = 0; i < rowCount; i++) { if (i != r) m_rref->data[i][lead] -= m_rref->data[r][lead] * m_rref->data[i][lead]; //Subtract M[i, lead] multiplied by row r from row i } lead++; } return m_rref; }
void matrix_inverse(Matrix *X, Matrix *X_inverse, Matrix *Xsamedims) { int n=numrows(X), e_code, ipiv[n]; // Need to set X_inverse to the identity matrix on input: matrix_identity(X_inverse); // Copy X to Xsamedims (error check for dims inside matrix_copy): matrix_copy(X, Xsamedims); // Compute: Solution to a real system of linear equations: A * X = B // Where A is an N-by-N matrix and X and B are N-by-NRHS matrices. // The LU decomposition with partial pivoting and row interchanges is // used to factor A as A = P * L * U, // where P is a permutation matrix, L is unit lower triangular, and U is // upper triangular. The factored form of A is then used to solve the // system of equations A * X = B. // // N = The number of linear equations, i.e., numrows(A) // NRHS = The number of right hand sides, i.e., numcols(B) // // A = LDA-by-N matrix, the leading N-by-N matrix of A is the // coefficient matrix A. On exit, the factors L and U from the // factorization. A = P*L*U // LDA = The leading dimension of the array A (LDA >= max(1,N)) // // IPIV = N-vector containing the pivot indices that define P; // row i of the matrix was interchanged with row IPIV(i) // // B = LDB-by-NRHS matrix, the leading N-by-NRHS matrix of B is the // right hand side matrix. On exit, the N-by-NRHS solution X. // // LDB = The leading dimension of the array B (LDB >= max(1,N)) // INFO =0 => Successful exit // <0 => If INFO = -i, the i-th argument had an illegal value // >0 => If INFO = i, U(i,i) is exactly zero. The factorization // has been completed, but the factor U is exactly // singular, so the solution could not be computed. //dgesv(n,n,Xsamedims,n,ipiv,X_inverse,n,&e_code); // C version F77_CALL(dgesv)(&n,&n,Xsamedims,&n,ipiv,X_inverse,&n,&e_code); // R version if (!e_code) return; if (e_code<0) error("Singular value in mat_inverse.\n"); else error("Illegal value in mat_inverse.\n"); return; }
void matrix_stack_push(MatrixStack* ms) { mstack *m; mstackelem *e; m = (mstack*)ms; e = mstackelem_create(); matrix_copy(e->mat, m->top->mat); e->next = m->top; m->top = e; ++m->depth; }
static int ref_desc_create(ref_desc_t **_desc, TMatrix_CSR *matr, info_t info) { ref_desc_t *desc = malloc(sizeof(ref_desc_t)); if (desc == NULL) return DE(ERROR_MEMORY); int err = matrix_copy(matr, &desc->matr); if (err) { *_desc = NULL; free(desc); return DE(err); } desc->hash = REF_HASH; *_desc = desc; return ERROR_NO_ERROR; }
char set_level(matrix *p_matrix, summit *v_summit) { int i; int i_level = 0; matrix MatriceTemp; MatriceTemp.i_size = p_matrix->i_size; matrix_malloc(&MatriceTemp); if(has_loop(p_matrix)){ printf("Error : set_level. A matrix which have loop cannot be used to calculate levels"); return 0; } else{ //Initialisation matrix_copy(p_matrix, &MatriceTemp); for(i=0;i<MatriceTemp.i_size;i++){ v_summit[i].i_level = -1; } for(i=0;i<MatriceTemp.i_size;i++){ //Les sommets sans precedents sont de NV 0 if(has_prev(&MatriceTemp, i)){ v_summit[i].i_level = i_level; } } for(i=0;i<MatriceTemp.i_size;i++){ if(v_summit[i].i_level == i_level){ matrix_zero_at_line(&MatriceTemp, i); } } //Fin init while(!all_level_checked(p_matrix, v_summit)){ i_level++; for(i=0;i<MatriceTemp.i_size;i++){ if(has_prev(&MatriceTemp, i)&&(v_summit[i].i_level==-1)){ v_summit[i].i_level = i_level; } } for(i=0;i<MatriceTemp.i_size;i++){ if(v_summit[i].i_level == i_level){ matrix_zero_at_line(&MatriceTemp, i); } } //Fin for } //Fin while } //Fin si has_loop matrix_free(&MatriceTemp); return 1; }
/* * Allocate an Element and store a duplicate of the data pointed to by * obj in the Element. Modules do not get duplicated. The function needs * to handle each type of object separately in a case statement. */ Element *element_init(ObjectType type, void *obj){ Element *e = malloc(sizeof(Element)); if(!e){ printf("malloc failed in element_init\n"); return NULL; } e->type = type; e->next = NULL; switch (e->type) { case ObjNone: printf("ObjNone not implemented in element_init\n"); break; case ObjLine: line_copy(&(e->obj.line), (Line*)obj); break; case ObjPoint: point_copy(&(e->obj.point), (Point*)obj); break; case ObjPolyline: polyline_init(&(e->obj.polyline)); polyline_copy(&(e->obj.polyline), (Polyline*)obj); break; case ObjPolygon: polygon_init(&(e->obj.polygon)); polygon_copy(&(e->obj.polygon), (Polygon*)obj); break; case ObjIdentity: break; case ObjMatrix: matrix_copy(&(e->obj.matrix), (Matrix*)obj); break; case ObjColor: case ObjBodyColor: case ObjSurfaceColor: color_copy(&(e->obj.color), (Color*)obj); break; case ObjSurfaceCoeff: e->obj.coeff = *(float*)obj; break; case ObjLight: printf("ObjLight not implemented in element_init\n"); break; case ObjModule: e->obj.module = obj; break; default: printf("ObjectType %d is not handled in element_init\n",type); } return e; }