void calc_compute(op_t *op) { if (op->type != OP_CMP) return; op_t *cop = (op_t *) op->arg1; switch (cop->type) { case OP_ADD: calc_add(cop); break; case OP_SUB: calc_sub(cop); break; case OP_MUL: calc_mul(cop); break; case OP_DIV: calc_div(cop); break; case OP_MOD: calc_mod(cop); break; default: break; } }
/* * X * p2 = p1, p1/p2 = X, Error */ Matrix* calc_div(Matrix* p1, Matrix* p2) { Matrix *oldAns = ans; if (p1 == NULL || p2 == NULL) { //Error return NULL; } ans = NULL; if (!calc_mul(p1, calc_inverse(p2))) { //Error return NULL; } stor_freeMatrix(oldAns); return ans; }
int main(int argc,char **argv){ unsigned int calc_mod = 0; double idata = 0; double kdata = 0; double result = 0; if(argc <2){ printf("enter cmd numbers must >= 2!!\n"); return -1; } if(2 == argc){ argv++; calc_mod = atoi(*argv); switch(calc_mod){ case 5: printf("Calc PI is %f\n",calc_pi()); break; case 14: result = calc_rand(); printf("Calc rand result is %f\n", result); break; default: printf("Input abnormally\n"); break; } } if(3 == argc){ argv++; calc_mod = atoi(*argv); argv++; idata = atof(*argv); switch(calc_mod){ case 6: result = calc_sin((double) idata); printf("Calc sin (%f) result is %f\n",idata,result); break; case 7: result= calc_cos((double) idata); printf("Calc cos (%f) result is %f\n",idata,result); break; case 8: if(PI/2 == idata ||-PI/2 == idata){ printf("Input abnormally\n"); break; } result = calc_tan((double) idata); printf("Calc tan (%f) result is %f\n",idata,result); break; case 9: result = calc_fabs((double) idata); printf("Calc tan (%f) result is %f\n",idata,result); break; case 10: result = calc_exp((double) idata); printf("Calc exp (%f) result is %f\n",idata,result); break; case 11: if(idata <= 0){ printf("Input abnormally\n"); break; } result = calc_ln((double) idata); printf("Calc ln (%f) result is %f\n",idata,result); break; case 12: if(idata <= 0){ printf("Input abnormally\n"); break; } result = calc_log10((double) idata); printf("Calc log10 (%f) result is %f\n",idata,result); break; default: printf("Input abnormally\n"); break; } } if(4 == argc){ argv++; calc_mod = atoi(*argv); argv++; idata = atof(*argv); argv++; kdata = atof(*argv); switch(calc_mod){ case 1: result = calc_add(idata, kdata); printf("Calc add (%f + %f) result is %f\n", idata, kdata, result); break; case 2: result = calc_sub(idata, kdata); printf("Calc sub (%f - %f) result is %f\n", idata, kdata, result); break; case 3: result = calc_mul(idata,kdata); printf("Calc mul (%f * %f) result is %f\n", idata, kdata, result); break; case 4: if(0 == kdata){ printf("Input abnormaly\n"); break; } result = calc_div(idata,kdata); printf("Calc div (%f / %f) result is %f\n", idata, kdata, result); case 13: result = calc_pow(idata,kdata); printf("Calc pow (%f / %f) result is %f\n", idata, kdata, result); break; default: printf("Input abnormally\n"); break; } } return 0; }
/* * Error */ Matrix* calc_ex(Matrix* p,long ex) { Matrix *oldAns = ans; Matrix *temp = NULL, *temp2 = NULL; int n; if (p == NULL) { //Error return NULL; } if (p->m != p->n) { //Error return NULL; } n = p->m; if (ex < 0) { ans = NULL; if (!(temp = calc_inverse(p))) { //Error return NULL; } ans = NULL; ex = -ex; } else { if (!stor_createMatrix(&temp, n, n)) { //Error return NULL; } if (!stor_assign(temp, p)) { //Error return NULL; } ans = NULL; } if (!calc_eye(n)) { //Error return NULL; } while (ex) { if (ex % 2) { if (!calc_mul(temp, ans)) { //Error return NULL; } } temp2 = ans; //protect ans ans = NULL; //protect ans if (!calc_mul(temp, temp)) { //Error return NULL; } stor_freeMatrix(temp); temp = ans; ans = temp2; //protect ans ex /= 2; } stor_freeMatrix(temp); stor_freeMatrix(oldAns); return ans; }
/* * the eigvalue, 有可能在数学上出问题(虚数) Error */ Matrix* calc_eig(Matrix* p) { Matrix *oldAns = ans, *oldAns2; Matrix *temp = NULL,*temp2 = NULL; Matrix *v = NULL; double r1; int i, j, k = 0; if (p == NULL) { //Error return NULL; } if (p->m != p->n) { //Error return NULL; } if (!stor_createMatrix(&v, p->m, 1)) { //Error return NULL; } if (!stor_createMatrix(&temp, p->m, p->n)) { //Error return NULL; } if (!stor_createMatrix(&temp2, p->m, p->n)) { //Error return NULL; } if (!stor_assign(temp, p)) { //Error return NULL; } for (i = 0; i < p->n - 2; i++) { for (j = 0; j < p->m; j++) { *stor_entry(v, j, 0) = *stor_entry(temp, j, i); } if (!householder(v, i + 1)) { //Error return NULL; } oldAns2 = ans; ans = NULL; if (!calc_mul(calc_mul(oldAns2, temp), oldAns2)) //H*A*H { //Error return NULL; } stor_freeMatrix(temp); temp = ans; ans = NULL; stor_freeMatrix(oldAns2); } //temp is 拟三角阵 while (k<=500 && !isUpTrian(temp))//最多500次迭代 { k++; ans = NULL; if (!(temp2 = calc_eye(p->n))) { //Error return NULL; } ans = NULL; for (i = 0; i < p->n - 1; i++) { if (!calc_eye(p->n)) { //Error return NULL; } r1 = sqrt(*stor_entry(temp, i, i) * *stor_entry(temp, i, i) + *stor_entry(temp, i + 1, i)* *stor_entry(temp, i + 1, i)); if (util_isZero(r1)) { //Error return NULL; } *stor_entry(ans, i, i) = *stor_entry(temp, i, i) / r1; *stor_entry(ans, i + 1, i + 1) = *stor_entry(temp, i, i) / r1; *stor_entry(ans, i + 1, i) = -*stor_entry(temp, i + 1, i) / r1; *stor_entry(ans, i, i + 1) = *stor_entry(temp, i + 1, i) / r1; oldAns2 = ans; ans = NULL; if (!calc_mul(temp2, calc_trans(oldAns2)))//Q { //Error return NULL; } stor_freeMatrix(temp2); temp2 = ans; ans = NULL; if (!calc_mul(oldAns2, temp))//R { //Error return NULL; } stor_freeMatrix(temp); temp = ans; ans = NULL; stor_freeMatrix(oldAns2); } if (!calc_mul(temp, temp2)) { //Error return NULL; } stor_freeMatrix(temp); temp = ans; } //优化迭代, 好像可以证明RQ一定是拟上三角阵 for (i = 0; i < p->m; i++) { for (j = 0; j < p->n; j++) { if (i != j) *stor_entry(ans, i, j) = 0; } } return ans; }
/* * H*x will make [c+2~m] zero, c is script, Error */ static Matrix* householder(Matrix *v, int c) { Matrix* oldAns = ans; Matrix* oldAns2; Matrix* temp = NULL; int i; double norm; int k; if (v == NULL) { //Error return NULL; } if (v->n != 1) { //Error return NULL; } ans = NULL; if (!stor_createMatrix(&temp, v->m, 1)) { //Error return NULL; } if (!stor_assign(temp, v)) { //Error return NULL; } for (i = 0; i < c; i++) { *stor_entry(temp, i, 0) = 0; } if (!calc_norm(temp))//NULL { //Error return NULL; } norm = *stor_entry(ans, 0, 0); k = c; *stor_entry(temp, k, 0) += norm; if (!calc_norm(temp)) { //Error return NULL; } norm = *stor_entry(ans, 0, 0); if (util_isZero(norm)) { //Error return NULL; } if (!calc_numMul(calc_mul(temp, calc_trans(temp)),(double)2.0/(norm*norm))) { //Error return NULL; } oldAns2 = ans; ans = NULL; if (!calc_sub(calc_eye(temp->m), oldAns2)) { //Error return NULL; } stor_freeMatrix(oldAns2); stor_freeMatrix(oldAns); return ans; }