// 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; }
/* * draws the swarm using the given view etc into the given image */ void swarm_draw(Swarm *s, Matrix *VTM, Matrix *GTM, DrawState *ds, Lighting *lighting, Image *src){ int i, verbose = 0; if(verbose) printf("drawing swarm\n"); Matrix m, transGTM; if (!s){ printf("no swarm to draw!\n"); } // draw actors for (i = 0; i < s->numActors; i++){ matrix_identity(&m); matrix_translate(&m,s->actors[i].location.val[0], s->actors[i].location.val[1], s->actors[i].location.val[2]); matrix_multiply(&m, GTM, &transGTM); module_draw(s->actors[i].shape, VTM, &transGTM, ds, lighting, src); } // draw leaders for (i = 0; i < s->numLeaders; i++){ matrix_identity(&m); matrix_translate(&m,s->leaders[i].location.val[0], s->leaders[i].location.val[1], s->leaders[i].location.val[2]); matrix_multiply(&m, GTM, &transGTM); module_draw(s->leaders[i].shape, VTM, &transGTM, ds, lighting, src); } if(verbose) printf("swarm drawn\n"); }
static void lakka_draw_icon(lakka_handle_t *lakka, GLuint texture, float x, float y, float alpha, float rotation, float scale) { struct gl_coords coords; if (!lakka) return; if (alpha > lakka->global_alpha) alpha = lakka->global_alpha; if (alpha == 0) return; gl_t *gl = (gl_t*)driver_video_resolve(NULL); if (!gl) return; if (x < -lakka->icon_size || x > gl->win_width + lakka->icon_size || y < -lakka->icon_size || y > gl->win_height + lakka->icon_size) return; GLfloat color[] = { 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, }; if (gl->shader && gl->shader->use) gl->shader->use(gl, GL_SHADER_STOCK_BLEND); glViewport(x, gl->win_height - y, lakka->icon_size, lakka->icon_size); coords.vertices = 4; coords.vertex = lakka_vertex; coords.tex_coord = lakka_tex_coord; coords.lut_tex_coord = lakka_tex_coord; coords.color = color; glBindTexture(GL_TEXTURE_2D, texture); math_matrix mymat; math_matrix mrot; matrix_rotate_z(&mrot, rotation); matrix_multiply(&mymat, &mrot, &gl->mvp_no_rot); math_matrix mscal; matrix_scale(&mscal, scale, scale, 1); matrix_multiply(&mymat, &mscal, &mymat); gl->shader->set_coords(&coords); gl->shader->set_mvp(gl, &mymat); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisable(GL_BLEND); }
static void vtn_handle_matrix_alu(struct vtn_builder *b, SpvOp opcode, struct vtn_value *dest, struct vtn_ssa_value *src0, struct vtn_ssa_value *src1) { switch (opcode) { case SpvOpFNegate: { dest->ssa = vtn_create_ssa_value(b, src0->type); unsigned cols = glsl_get_matrix_columns(src0->type); for (unsigned i = 0; i < cols; i++) dest->ssa->elems[i]->def = nir_fneg(&b->nb, src0->elems[i]->def); break; } case SpvOpFAdd: { dest->ssa = vtn_create_ssa_value(b, src0->type); unsigned cols = glsl_get_matrix_columns(src0->type); for (unsigned i = 0; i < cols; i++) dest->ssa->elems[i]->def = nir_fadd(&b->nb, src0->elems[i]->def, src1->elems[i]->def); break; } case SpvOpFSub: { dest->ssa = vtn_create_ssa_value(b, src0->type); unsigned cols = glsl_get_matrix_columns(src0->type); for (unsigned i = 0; i < cols; i++) dest->ssa->elems[i]->def = nir_fsub(&b->nb, src0->elems[i]->def, src1->elems[i]->def); break; } case SpvOpTranspose: dest->ssa = vtn_ssa_transpose(b, src0); break; case SpvOpMatrixTimesScalar: if (src0->transposed) { dest->ssa = vtn_ssa_transpose(b, mat_times_scalar(b, src0->transposed, src1->def)); } else { dest->ssa = mat_times_scalar(b, src0, src1->def); } break; case SpvOpVectorTimesMatrix: case SpvOpMatrixTimesVector: case SpvOpMatrixTimesMatrix: if (opcode == SpvOpVectorTimesMatrix) { dest->ssa = matrix_multiply(b, vtn_ssa_transpose(b, src1), src0); } else { dest->ssa = matrix_multiply(b, src0, src1); } break; default: unreachable("unknown matrix opcode"); } }
void lakka_draw_icon(GLuint texture, float x, float y, float alpha, float rotation, float scale) { GLfloat color[] = { 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, 1.0f, 1.0f, 1.0f, alpha, }; static const GLfloat vtest[] = { 0, 0, 1, 0, 0, 1, 1, 1 }; gl_t *gl = (gl_t*)driver.video_data; if (!gl) return; if (alpha > global_alpha) alpha = global_alpha; glViewport(x, gl->win_height - y, dim, dim); glEnable(GL_BLEND); gl->coords.vertex = vtest; gl->coords.tex_coord = vtest; gl->coords.color = color; glBindTexture(GL_TEXTURE_2D, texture); if (gl->shader && gl->shader->use) gl->shader->use(gl, GL_SHADER_STOCK_BLEND); math_matrix mymat; math_matrix mrot; matrix_rotate_z(&mrot, rotation); matrix_multiply(&mymat, &mrot, &gl->mvp_no_rot); math_matrix mscal; matrix_scale(&mscal, scale, scale, 1); matrix_multiply(&mymat, &mscal, &mymat); gl->coords.vertices = 4; gl_shader_set_coords(gl, &gl->coords, &mymat); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisable(GL_BLEND); gl->coords.vertex = gl->vertex_ptr; gl->coords.tex_coord = gl->tex_coords; gl->coords.color = gl->white_color_ptr; }
Correction * back_propagate( NetworkState *network_state, Activation *activity, Matrix expected ) { int32_t layers = network_state->layers; int32_t last_layer = layers - 1; Correction *correction = correction_new(layers - 1); Matrix error, weight_correction, input_gradient, output_gradient; error = network_state->error( expected, activity->output[last_layer], activity->input[last_layer], network_state->derivative[last_layer] ); correction->biases[last_layer - 1] = error; weight_correction = matrix_new( network_state->weights[last_layer][0], network_state->weights[last_layer][1] ); matrix_dot_tn(activity->output[last_layer - 1], error, weight_correction); correction->weights[last_layer - 1] = weight_correction; for (int32_t layer = layers - 2; layer > 0; layer -= 1) { output_gradient = matrix_new(error[0], network_state->weights[layer + 1][0]); matrix_dot_nt(error, network_state->weights[layer + 1], output_gradient); input_gradient = matrix_new(activity->input[layer][0], activity->input[layer][1]); matrix_clone(input_gradient, activity->input[layer]); activation_closure_call(network_state->derivative[layer], input_gradient); error = matrix_new(network_state->biases[layer][0], network_state->biases[layer][1]); matrix_multiply(output_gradient, input_gradient, error); if (activity->mask[layer] != NULL) { matrix_multiply(error, activity->mask[layer], error); } correction->biases[layer - 1] = error; weight_correction = matrix_new( network_state->weights[layer][0], network_state->weights[layer][1] ); matrix_dot_tn(activity->output[layer - 1], error, weight_correction); correction->weights[layer - 1] = weight_correction; } return correction; }
double accuracy(matrix_list_t* theta, matrix_t* X, matrix_t* y) { assert(theta->num == 2); matrix_t* theta_transpose, *temp, *temp2; theta_transpose = matrix_transpose(theta->matrix_list[0]); temp = matrix_prepend_col(X, 1.0); temp2 = matrix_multiply(temp, theta_transpose); matrix_t* h1 = matrix_sigmoid(temp2); free_matrix(theta_transpose); free_matrix(temp); free_matrix(temp2); theta_transpose = matrix_transpose(theta->matrix_list[1]); temp = matrix_prepend_col(h1, 1.0); temp2 = matrix_multiply(temp, theta_transpose); matrix_t* h2 = matrix_sigmoid(temp2); free_matrix(theta_transpose); free_matrix(temp); free_matrix(temp2); assert(h2->rows == 5000 && h2->cols == 10); matrix_t* p = matrix_constructor(1, 5000); int i, j; for(i = 0; i<h2->rows; i++) { double max = 0.0; unsigned char first = 1; for(j=0; j<h2->cols; j++) { if(matrix_get(h2, i, j) > max || first == 1) { vector_set(p, i, j); max = matrix_get(h2, i, j); first = 0; } } } double count = 0; for(i=0; i<5000; i++) { if(vector_get(y, i) == vector_get(p, i)) count = count + 1; } free_matrix(p); free_matrix(h1); free_matrix(h2); return count/5000; }
void refine(double *target, double *var) { int i, j; double *residual = work.buffer; double norm2; double *new_var = work.buffer2; for (j = 0; j < settings.refine_steps; j++) { norm2 = 0; matrix_multiply(residual, var); for (i = 0; i < 179; i++) { residual[i] = residual[i] - target[i]; norm2 += residual[i]*residual[i]; } #ifndef ZERO_LIBRARY_MODE if (settings.verbose_refinement) { if (j == 0) printf("Initial residual before refinement has norm squared %.6g.\n", norm2); else printf("After refinement we get squared norm %.6g.\n", norm2); } #endif /* Solve to find new_var = KKT \ (target - A*var). */ ldl_solve(residual, new_var); /* Update var += new_var, or var += KKT \ (target - A*var). */ for (i = 0; i < 179; i++) { var[i] -= new_var[i]; } } #ifndef ZERO_LIBRARY_MODE if (settings.verbose_refinement) { /* Check the residual once more, but only if we're reporting it, since */ /* it's expensive. */ norm2 = 0; matrix_multiply(residual, var); for (i = 0; i < 179; i++) { residual[i] = residual[i] - target[i]; norm2 += residual[i]*residual[i]; } if (j == 0) printf("Initial residual before refinement has norm squared %.6g.\n", norm2); else printf("After refinement we get squared norm %.6g.\n", norm2); } #endif }
/** * Check left and right multiplication by the identity */ int test_identity() { float i[4] = {1, 0, 0, 1}; float a[4] = {1, 2, 3, 4}; float b[4]; matrix_multiply(i, a, b, 2, 2, 2); ASSERT(!float_cmp_array(b, a, TOLERANCE, 4)); matrix_multiply(a, i, b, 2, 2, 2); ASSERT(!float_cmp_array(b, a, TOLERANCE, 4)); return 1; }
int main(int argc, char const* argv[]) { /* Number of rows */ int mrow = 3; /* Number of columns */ int mcol = 4; /* Number of rows */ int nrow = 4; /* Number of columns */ int ncol = 2; printf("m\n"); int ** m = array(mcol, mrow); printf("n\n"); int ** n = array(ncol, nrow); int ** result = matrix_multiply(m, mcol, mrow, n, ncol, nrow); int i, j; for (i = 0; i < mrow; i++) { for (j = 0; j < ncol; j++) { printf("%3d ", result[i][j]); } printf("\n"); } return 0; }
int main(void) { enum { N = 3, M = 4, P = 4, Q = 5 }; int A[N][M] = { { 41, 76, 70, 42, }, { 70, 62, 77, 74, }, { 49, 55, 43, 65, }, }; int B[P][Q] = { { 73, 33, 42, 72, 65, }, { 69, 30, 83, 83, 64, }, { 90, 74, 84, 51, 23, }, { 62, 45, 84, 46, 43, }, }; static_assert(M == P, "Matrix dimensions are mismatched"); // C11 int (*C)[Q]; print_matrix("A", 3, N, M, A); print_matrix("B", 3, P, Q, B); C = matrix_multiply(N, M, Q, A, B); print_matrix("C", 6, N, Q, C); free(C); return 0; }
END_TEST START_TEST(test_matrix_inverse_3x3) { u32 i, j, t; double A[9]; double B[9]; double I[9]; seed_rng(); /* 3x3 inverses */ for (t = 0; t < LINALG_NUM; t++) { do { for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) A[3*i + j] = mrand; } while (matrix_inverse(3, A, B) < 0); matrix_multiply(3, 3, 3, A, B, I); fail_unless(fabs(I[0] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[0]); fail_unless(fabs(I[4] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[4]); fail_unless(fabs(I[8] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[8]); } for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) if (j == 0) A[3*i + j] = 33; else A[3*i + j] = 1; s32 mi = matrix_inverse(3, A, B); fail_unless(mi < 0, "Singular matrix not detected."); }
END_TEST START_TEST(test_matrix_inverse_4x4) { u32 i, j, t; double A[16]; double B[16]; double I[16]; seed_rng(); /* 4x4 inverses */ for (t = 0; t < LINALG_NUM; t++) { do { for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) A[4*i + j] = mrand; } while (matrix_inverse(4, A, B) < 0); matrix_multiply(4, 4, 4, A, B, I); fail_unless(fabs(I[0] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[0]); fail_unless(fabs(I[5] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[5]); fail_unless(fabs(I[10] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[10]); fail_unless(fabs(I[15] - 1) < LINALG_TOL, "Matrix differs from identity: %lf", I[15]); } for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (j == 0) A[4*i + j] = 44; else A[4*i + j] = 1; s32 mi = matrix_inverse(4, A, B); fail_unless(mi < 0, "Singular matrix not detected."); }
static void compute_dops(const double H[4][4], const double pos_ecef[3], dops_t *dops) { /* PDOP is the norm of the position elements of tr(H) */ double pdop_sq = H[0][0] + H[1][1] + H[2][2]; dops->pdop = sqrt(pdop_sq); /* TDOP is like PDOP but for the time state. */ dops->tdop = sqrt(H[3][3]); /* Calculate the GDOP -- ||tr(H)|| = sqrt(PDOP^2 + TDOP^2) */ dops->gdop = sqrt(pdop_sq + H[3][3]); /* HDOP and VDOP are Horizontal and Vertical. We could rotate H * into NED frame and then take the separate components, but a more * computationally efficient approach is to find the vector in the * ECEF frame that represents the Down unit vector, and project it * through H. That gives us VDOP^2, then we find HDOP from the * relation PDOP^2 = HDOP^2 + VDOP^2. */ double M[3][3]; ecef2ned_matrix(pos_ecef, M); double down_ecef[4] = {M[2][0], M[2][1], M[2][2], 0}; double tmp[3]; matrix_multiply(3, 4, 1, (double *)H, down_ecef, tmp); double vdop_sq = vector_dot(3, down_ecef, tmp); dops->vdop = sqrt(vdop_sq); dops->hdop = sqrt(pdop_sq - vdop_sq); }
uint select_poly_rotate(double *matrix, double *v_a, double *pos_a, double *v_b, double *pos_b, uint poly) { double r, matrix2[16], matrix3[16], pos[] = {0, 0, 0}, best = -1; uint dir_a = 0, dir_b = 0, i, j; for(i = 0; i < poly; i++) { for(j = 0; j < poly; j++) { r = v_a[i * 3 + 0] * v_b[j * 3 + 0] + v_a[i * 3 + 1] * v_b[j * 3 + 1] + v_a[i * 3 + 2] * v_b[j * 3 + 2]; if(r > best) { best = r; dir_a = i; dir_b = j; } } } create_matrix_normalized(matrix2, pos, &v_a[dir_a * 3], &v_a[((poly + dir_a - 1) % poly) * 3]); create_matrix_normalized(matrix3, pos, &v_b[dir_b * 3], &v_b[((poly + dir_b + 1) % poly) * 3]); negate_matrix(matrix2); matrix_multiply(matrix3, matrix2, matrix); return (poly + dir_a + dir_b + 2) % poly; }
extern void matrix_solve(vector* v, const matrix* m) { assert(v != NULL); assert(m != NULL); assert(v->size == m->size); size_t size = m->size; vector tmp_vec; vector_init(&tmp_vec, size); for (size_t i = 0; i < size; ++i) { v->elements[i] = 1.0; tmp_vec.elements[i] = 0.0; } for(size_t x = 0; x < 3; ++x) { matrix_multiply(&tmp_vec, m, v); vector_normalize(&tmp_vec); for (size_t i = 0; i < size; ++i) { v->elements[i] = tmp_vec.elements[i]; tmp_vec.elements[i] = 0.0; } } vector_free(&tmp_vec); }
// remember to normalize vector (x, y, z), and to convert angle from degree to radius before calling sin and cos void I_my_glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) { GLdouble rad,c,s,rz[16]; printf("Call to I_my_glRotated\n"); rad = angle*PI/180; c = cos(rad); s = sin(rad); normalize(&x,&y,&z); rz[0] = x*x*(1-c)+c; rz[1] = y*x*(1-c)+z*s; rz[2] = x*z*(1-c)-y*s; rz[3] = 0; rz[4] = x*y*(1-c)-z*s; rz[5] = y*y*(1-c)+c; rz[6] = y*z*(1-c)+x*s; rz[7] = 0; rz[8] = x*z*(1-c)+y*s; rz[9] = y*z*(1-c)-x*s; rz[10] = z*z*(1-c)+c; rz[11] = 0; rz[12] = 0; rz[13] = 0; rz[14] = 0; rz[11] = 0; rz[15] = 1; matrix_multiply(rz); }
int main(int argc, char **argv) { int heap=3000000, stack=3000000; int me, nprocs, i; double start, end; /* Initialize MPI */ MPI_Init(&argc, &argv); /* Initialize GA */ GA_Initialize(); /* Initialize Memory Allocator (MA) */ if(! MA_init(C_DBL, stack, heap) ) GA_Error("MA_init failed",stack+heap); me = GA_Nodeid(); nprocs = GA_Nnodes(); if(me==0) { printf("\nUsing %d processes\n\n", nprocs); fflush(stdout); } start = MPI_Wtime(); for(i =0; i< 1000; i++) { matrix_multiply(); } end = MPI_Wtime(); if(me==0) printf(" Time=%2.5e secs\n\n",end-start); if(me==0)printf("\nTerminating ..\n"); GA_Terminate(); MPI_Finalize(); }
void camtrans_compute_matrices (CamTrans *t) { double rot[9]; double rot_trans[9]; rot_quat_to_matrix(t->orientation, rot); matrix_transpose_3x3d (rot, rot_trans); double tmp_34[] = { 1, 0, 0, -t->position[0], 0, 1, 0, -t->position[1], 0, 0, 1, -t->position[2], }; double pinhole[] = { t->fx, t->skew, t->cx, 0, t->fy, t->cy, 0, 0, 1 }; double tmp_33[9]; matrix_multiply_3x3_3x3 (pinhole, rot_trans, tmp_33); matrix_multiply (tmp_33, 3, 3, tmp_34, 3, 4, t->matx); double m[9] = { t->matx[0], t->matx[1], t->matx[2], t->matx[4], t->matx[5], t->matx[6], t->matx[8], t->matx[9], t->matx[10] }; int status = matrix_inverse_3x3d (m, t->inv_matx); if (0 != status) { fprintf (stderr, "WARNING: camera matrix is singular (%s:%d)\n", __FILE__, __LINE__); } }
/** Converts a vector in local North, East, Down (NED) coordinates relative to * a reference point given in WGS84 Earth Centered, Earth Fixed (ECEF) Cartesian * coordinates to a vector in WGS84 ECEF coordinates. * * Note, this function only \e rotates the NED vector into the ECEF frame, as * would be appropriate for e.g. a velocity vector. To pass an NED position in * the reference frame of the NED, see \ref wgsned2ecef_d. * * \see wgsned2ecef_d. * * \param ned The North, East, Down vector is passed as [N, E, D], all in * meters. * \param ref_ecef Cartesian coordinates of the reference point, passed as * [X, Y, Z], all in meters. * \param ecef Cartesian coordinates of the point written into this array, * [X, Y, Z], all in meters. */ void wgsned2ecef(const double ned[3], const double ref_ecef[3], double ecef[3]) { double M[3][3], M_transpose[3][3]; ecef2ned_matrix(ref_ecef, M); matrix_transpose(3, 3, (double *)M, (double *)M_transpose); matrix_multiply(3, 3, 1, (double *)M_transpose, ned, ecef); }
/** Converts a vector in WGS84 Earth Centered, Earth Fixed (ECEF) Cartesian * coordinates to the local North, East, Down (NED) frame of a reference point, * also given in WGS84 ECEF coordinates. * * Note, this function only \e rotates the ECEF vector into the NED frame of * the reference point, as would be appropriate for e.g. a velocity vector. To * determine the distance between the point and the reference point in the NED * frame of the reference point, see \ref wgsecef2ned_d. * * \see wgsecef2ned_d. * * \param ecef Cartesian coordinates of the point, passed as [X, Y, Z], * all in meters. * \param ref_ecef Cartesian coordinates of the reference point, passed as * [X, Y, Z], all in meters. * \param ned The North, East, Down vector is written into this array as * [N, E, D], all in meters. */ void wgsecef2ned(const double ecef[3], const double ref_ecef[3], double ned[3]) { double M[3][3]; ecef2ned_matrix(ref_ecef, M); matrix_multiply(3, 3, 1, (double *)M, ecef, ned); }
int main(int argc, char **argv) { //check for the correct number of input arguments if(argc != 4){ fprintf(stderr, "There should be 3 arguments, nrowA (integer), ncolA (integer), and ncolB (integer) \n"); fprintf(stderr, "Usage: %s nrowA ncolA ncolB \n", argv[0]); return 1;} int nrowA = atoi(argv[1]); int ncolA = atoi(argv[2]); int nrowB = ncolA; int ncolB = atoi(argv[3]); int nrowC = nrowA; int ncolC = ncolB; //initialize matrices AST523_MATRIX *A = AST523_matrixNew(nrowA, ncolA); AST523_MATRIX *B = AST523_matrixNew(nrowB, ncolB); AST523_MATRIX *C = AST523_matrixNew(nrowC, ncolC); sin_init(A); sin_init(B); //perform multiplication matrix_multiply(A,B,C); //print output print_max(C); //clear memory AST523_matrixDel(A); AST523_matrixDel(B); AST523_matrixDel(C); return 0; }
// ----------------------------------------------------------- matrix_ortho --- matrix_t * matrix_ortho( matrix_t *self, float left, float right, float bottom, float top, float z_near, float z_far ) { assert( self ); float dx = right - left; float dy = top - bottom; float dz = z_far - z_near; if ( (dx == 0.0) || (dy == 0.0) || (dz == 0.0) ) { return self; } matrix_t ortho; matrix_load_identity( &ortho ); float *data = ortho.data; data[0*4+0] = 2.0 / dx; data[3*4+0] = -(right + left) / dx; data[1*4+1] = 2.0 / dy; data[3*4+1] = -(top + bottom) / dy; data[2*4+2] = -2.0 / dz; data[3*4+2] = -(z_near + z_far) / dz; data[3*4+3] = 1.0; matrix_multiply( &ortho, self ); memcpy( self, &ortho, sizeof( matrix_t ) ); return self; }
void multiply_top_stack(matrix * m){ matrix *copy = new matrix(); matrix *ptr = peek_stack(); copy_matrix(ptr,copy); matrix_multiply(copy,m,ptr); delete(copy); }
// --------------------------------------------------------- matrix_frustum --- matrix_t * matrix_frustum( matrix_t *self, float left, float right, float bottom, float top, float z_near, float z_far ) { assert( self ); float dx = right - left; float dy = top - bottom; float dz = z_far - z_near; if ( (z_near <= 0.0) || (z_far <= 0.0) || (dx <= 0.0) || (dy <= 0.0) || (dz <= 0.0) ) { return self; } matrix_t frustum; matrix_load_identity( &frustum ); float *data = frustum.data; data[0*4+0] = 2.0 * z_near / dx; data[1*4+1] = 2.0 * z_near / dy; data[2*4+0] = +(right + left) / dx; data[2*4+1] = +(top + bottom) / dy; data[2*4+2] = -(z_near + z_far) / dz; data[2*4+3] = -1.0; data[3*4+2] = -2.0 * z_near * z_far / dz; matrix_multiply( &frustum, self ); memcpy( self, &frustum, sizeof( matrix_t ) ); return self; }
Matrix* matrix_qr_decomposition(Matrix M) { assert(is_matrix(M)); int m = M->r; int n = M->c; Matrix Q = matrix_new_empty(m, n); for(int i = 0; i < n; i++) { double* u = matrix_column_vector(M, i); double* a = matrix_column_vector(M, i); for(int k = 0; k < i; k++) { double* e = matrix_column_vector(Q, k); double* p = vector_projection(e, a, m); free(e); for(int j = 0; j < m; j++) u[j] -= p[j]; free(p); } free(a); double* e = vector_normalize(u, m); free(u); for(int l = 0; l < m; l++) Q->A[l][i] = e[l]; free(e); } Matrix Qt = matrix_transpose(Q); Matrix R = matrix_multiply(Qt, M); matrix_free(Qt); Matrix* QR = calloc(2, sizeof(Matrix)); QR[0] = Q; QR[1] = R; return QR; }
/** * Get a translation matrix for this object. **/ void object_get_transform_mat(object_t *object, float matrix[16]) { MATRIX_DECL(translate, object->scale[0], 0, 0, object->trans[0], 0, object->scale[1], 0, object->trans[1], 0, 0, object->scale[2], object->trans[2], 0, 0, 0, 1); float rotate[16]; float translate_final[16]; quat_to_matrix(&object->rot, rotate); matrix_multiply(translate, rotate, translate_final); matrix_multiply(translate_final, object->pretransform, matrix); }
void matrix_rotate( Matrix *matrix, Vector_Elements axis, float angle ) { float _cos = (float) cos( angle ); float _sin = (float) sin( angle ); Matrix tmp = identity; switch (axis) { case X: tmp.v[Y2] = _cos; tmp.v[Z2] = - _sin; tmp.v[Y3] = _sin; tmp.v[Z3] = _cos; break; case Y: tmp.v[X1] = _cos; tmp.v[Z1] = _sin; tmp.v[X3] = - _sin; tmp.v[Z3] = _cos; break; case Z: tmp.v[X1] = _cos; tmp.v[Y1] = - _sin; tmp.v[X2] = _sin; tmp.v[Y2] = _cos; break; default: break; } matrix_multiply( matrix, &tmp ); }
// main function int main(int argc, char **argv) { int correct; double run_time; double mflops; // initialize the matrices matrix_init(); // multiply and capture the runtime run_time = matrix_multiply(); // verify that the result is sensible correct = check_result(); // Compute the number of mega flops mflops = (2.0 * N * P * M) / (1000000.0 * run_time); printf("Order %d multiplication in %f seconds \n", ORDER, run_time); printf("Order %d multiplication at %f mflops\n", ORDER, mflops); // Display check results if (correct) { printf("\n Hey, it worked"); } else { printf("\n Errors in multiplication"); } printf("\n all done \n"); return 0; }
void I_my_glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) { float deltaX = right - left; float deltaY = top - bottom; float deltaZ = zFar - zNear; GLdouble frust[16]; if ( (zNear <= 0.0) || (zFar <= 0.0) || (deltaX <= 0.0) || (deltaY <= 0.0) || (deltaZ <= 0.0) ) return; frust[0] = 2.0 * zNear / deltaX; frust[1] = frust[2] = frust[3] = 0.0; frust[5] = 2.0 * zNear / deltaY; frust[4] = frust[6] = frust[7] = 0.0; frust[8] = (right + left) / deltaX; frust[9] = (top + bottom) / deltaY; frust[10] = -(zNear + zFar) / deltaZ; frust[11] = -1.0; frust[14] = -2.0 * zNear * zFar / deltaZ; frust[12] = frust[13] = frust[15] = 0.0; matrix_multiply(frust); }