The screenshot is not very relevant because I use as B1the I3 who is an orthonormal base but the coordinates of the array will be the same. I didnt have time to take another base to see the diference...
----------------------------------------------------------------------------------main.c
1 /* App that calculates the coordinates of an array V from base B1 to canonical base B if B1 is an orthonormal base.
2 B1= base that user input
3 V= the array with coordinates in base B1
4 C= the array V with coordinates in B base
5 A= the orthogonal matrix if B1 is an orthonormal base ( transition matrix from B to B1)
6 B2=the transposed matrix A ( transition matrix from B1 to B)
7 */
8 #include <stdio.h>
9 #include<stdlib.h>
10 #include <math.h>
11 #include "def.c"
12 #include "functii.c"
13 int main()
14 {printf("Numarul de vectori si de elemente ale vectorilor din B1 este:");scanf("%d %d",&B1.lin,&B1.col);
15 printf("Nr de elemente ale vectorului este:");scanf("%d",&V.elem);
16 B2.lin=B1.lin;
17 B2.col=B1.col;
18 A.lin=B1.lin;
19 A.col=B1.col;
20 C.elem=V.elem;
21 A.matr=(double **)malloc(A.lin*sizeof(double*));
22 B1.matr=(double **)malloc(B1.lin*sizeof(double*));
23 B2.matr=(double **)malloc(B2.lin*sizeof(double*));
24 V.vector=(double *)malloc(V.elem*sizeof(double));
25 C.vector=(double *)malloc(C.elem*sizeof(double));
26 for(i=0;i<B1.lin;i++) // aloca memorie pt elem din linia i
27 if((B1.matr[i]=(double *)malloc(B1.col*sizeof(double)))==NULL || (B2.matr[i]=(double *)malloc(B2.col*sizeof(double)))==NULL || (A.matr[i]=(double *)malloc(A.col*sizeof(double)))==NULL)
28 {
29 printf("
memorie insuficienta");
30 exit(1) ;
31 }
32 else
33 if(V.vector==NULL || C.vector==NULL)
34 {
35 printf("
memorie insuficienta");
36 exit(1) ;
37 }
38 printf("Elementele bazei sunt:
");
39 citire_matrice(B1);
40 printf("Elementele vectorului sunt:
");
41 citire_vector(V);
42 printf("Baza este:
");
43 afisare_matrice(B1);printf("
");
44 printf("vectorul este:
");
45 afisare_vector(V);printf("
");
46 matrice_ortogonala(A,B1);
47 transpunere(A,B2);
48 vector_matrice(V,C,B2);
49
50 free(A.matr);
51 free(B1.matr);
52 free(B2.matr);
53 free(V.vector);
54 free(C.vector);
55 system("pause");
56 return 0;
57 }----------------------------------------------------------------------------------
def.c
1 typedef struct{
2 double **matr;
3 int lin;
4 int col;
5 }Matrice;
6 Matrice A,B1,B2;
7 typedef struct{
8 double *vector;
9 int elem;
10 }Array;
11 Array V,C;
12 int i,j;
13
14 ----------------------------------------------------------------------------------
functii.c
1 // citire matrice
2 void citire_matrice(Matrice A){
3 for(i=0;i<A.lin;i++)
4 for(j=0;j<A.col;j++)
5 scanf("%lf",&A.matr[j][i]);
6 }
7 //afisare matrice
8 void afisare_matrice(Matrice A){
9 for(i=0;i<A.lin;i++)
10 {for(j=0;j<A.col;j++)
11 printf(" %.2lf",A.matr[i][j]);
12 printf("
");
13 }
14 printf("
");
15 }
16
17 //test produs scalar=0
18 int produs_scalar(Matrice B){double suma=0;
19 for(i=0;i<B.lin;i++)
20 for(j=0;j<B.col;j++)
21 suma+=B.matr[i][j]*B.matr[i][j+1];
22 if(abs(suma)==0)
23 return 1;
24 else
25 return 0;
26 }
27 //test norma=1
28 int test_norma(Matrice B1){double norma=0;
29 for(i=0;i<B1.lin;i++)
30 {for(j=0;j<B1.col;j++)
31 norma+=pow(B1.matr[j][i],2);
32 if(sqrt(norma)==0)
33 return 0;
34
35 }
36 return 1;
37
38 }
39
40 //alcatuire matrice ortogonala
41 void matrice_ortogonala(Matrice A, Matrice B1){
42 if(produs_scalar(B1)==1 && test_norma(B1)==1)
43 {for(i=0;i<A.lin;i++)
44 for(j=0;j<A.col;j++)
45 A.matr[i][j]=B1.matr[i][j];
46 printf("matricea ortogonala este:
");
47 afisare_matrice(A);
48 }
49 else
50 printf("Matricea nu este ortogonala!");
51 }
52 // citire vector
53 void citire_vector(Array V){
54 for(i=0;i<V.elem;i++)
55 scanf("%lf",&V.vector[i]);
56 }
57 //afisare vector
58 void afisare_vector(Array V){
59 for(i=0;i<V.elem;i++)
60 printf(" %.2lf ",V.vector[i]);
61 printf("
");
62 }
63 //transpunere
64 void transpunere(Matrice A,Matrice B2){
65 for(i=0;i<A.lin;i++)
66 {for(j=0;j<A.col;j++)
67 B2.matr[i][j]=A.matr[j][i];
68
69 }
70 printf("
");
71 }
72
73
74 //coordonatele unui vector
75 void vector_matrice(Array V,Array C,Matrice B2){
76 if(V.elem!=B2.col)
77 printf("Inmultirea dintre vector si matrice nu se poate realiza!");
78 else{
79 for(i=0;i<B2.lin;i++)
80 for(j=0;j<B2.col;j++)
81 C.vector[i]+=B2.matr[i][j]*V.vector[j];
82 printf("Vectorul in noile coordonate este:");
83 afisare_vector(C);
84 }
85 }
0 comments:
Post a Comment