1 1.1 C 2 1 double a[ ][ ]; 1 3x3 0 1 3x3 ( ) 0.240 0.143 0.339 0.191 0.341 0.477 0.412 0.003 0.921 1.2 malloc() malloc 2 #include <stdio.h> #include <string.h> #include <stdlib.h> enum LENGTH = 10 ; int main() double *a; // a = ( double* )malloc( sizeof(double) * LENGTH ); // free( a ); return 0; 1
1: 1 1 double malloc double 1 2 double 1 3 double *a[ ]; malloc() double 1 malloc() free() 4 #include <stdio.h> #include <string.h> #include <stdlib.h> enum ROWS = 3, COLS = 3 ; int main() double *a[ ROWS ]; // a[ 0 ] = ( double* )malloc( sizeof(double) * ROWS * COLS ); for( int i = 1; i < ROWS; ++i ) a[ i ] = a[ 0 ] + COLS * i; // 2
free( a[ 0 ] ); return 0; 2: 2 ( ) 5 double **a; double 1 malloc() double 1 malloc() double 1 free() double 1 free() malloc() free() 2 malloc() 2 3 3
( ) a[i] = *(a+i) : a[i+j] = *( a + i + j ) = *( (i+a) + j ) = (i+a)[j] a[i][j] = *(a[i]+j) = *(*(a+i)+j) = (*(a+i))[j] 2 2 (rows) (cols) 2 MATRIX 6 struct MATRIX double** dat; // 2 int rows; // int cols; // ; 1. rows cols MATRIX make mat( int rows, int cols ); 2. void free mat( MATRIX A ); 3. void print mat( MATRIX A ); 7 MATRIX A; A = make_mat( 2, 2 ); A.dat[0][0] = 1; A.dat[0][1] = 2; A.dat[1][0] = 3; A.dat[1][1] = 4; print_mat( A ); free_mat( A ); 4 4
3 assert 1. (A B ) (B memmove() ) void copy mat( MATRIX A, MATRIX B ); 2. (A[ r from r to ][ c from c to ] B ) ( B ) void copy submat( MATRIX A, MATRIX B, int r from, int r to, int c from, int c to ); 3. (A T B ) ( B ) void t mat( MATRIX A, MATRIX B ); 4. (A±B C ) ( C ) void plus mat( MATRIX A, MATRIX B, MATRIX C ); minus mat( MATRIX A, MATRIX B, MATRIX C ); 5. (A*B C ) ( C ) void prod mat( MATRIX A, MATRIX B, MATRIX C ); 6. N ( A 1 B ) ( B A ) void inverse mat( MATRIX A, MATRIX B ); 3: 5 main main B 4 csv csv ( comma separated value : ) 1 ( 5
) (,) 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 csv 1.0, 2.0, 3.0 4.0, 5.0, 6.0 7.0, 8.0, 9.0 6 csv void write csv( char* filename, MATRIX A ); exit(1); 7 csv void read csv( char* filename, MATRIX A ); exit(1); MATRIX A make mat() 1. fopen() 2. fgets() 1 3. strtok() 4. atof() 5. 2 4 6. fclose() strtok() strtok() strtok() 8 char buf[ 1024 ]; strcpy( buf, "1.0, 2.0, 3.0" ); char *p; char *q = buf; for(;;) p = strtok( q, "," ); if(!p ) break; q = NULL; double val = atof( p ); fprintf( stdout, "%lf ", val ); 6
8 csv csv 7
A MATRIX make_mat( int rows, int cols ); void free_mat( MATRIX A ); // LU // // src -> LU // dst = LU // void lu_decomp( double** src, double** dst, int* pivot, int rows, int cols ) // src -> dst for( int r = 0; r < rows; ++r ) for( int c = 0; c < cols; ++c ) dst[ r ][ c ] = src[ r ][ c ]; for( int r =0; r < rows; ++r ) pivot[ r ] = r; int max_loop; if( rows > cols ) max_loop = rows; else max_loop = cols; for( int loop = 0; loop < max_loop; ++loop ) // pivot double maxval = dst[ pivot[ loop ] ][ loop ]; int maxrow = loop; for( int r = loop + 1 ; r < rows; ++r ) double tmpval = dst[ pivot[ r ] ][ loop ]; if( tmpval > maxval ) maxval = tmpval; maxrow = r; int tmprow = pivot[ loop ]; pivot[ loop ] = pivot[ maxrow ]; pivot[ maxrow ] = tmprow; // L // loop for( int r = loop + 1 ; r < rows; ++r ) dst[ pivot[ r ] ][ loop ] /= dst[ pivot[ loop ] ][ loop ]; // for( int r = loop + 1; r < rows; ++r ) for( int c = loop + 1; c < cols; ++c ) dst[ pivot[ r ] ][ c ] -= dst[ pivot[ r ] ][ loop ] * dst[ pivot[ loop ] ][ c ]; 8
// // Ax = y // // A[ N ][ N ] : N ( ) // y[ N ] : ( N ) // x[ N ] : ( N ) // void linear_equation( MATRIX A, double* x, double* y ) MATRIX LU; int N; int *pivot; double *z; if( A.cols!= A.rows ) fprintf( stderr, "linear_equation : \n" ); exit( 1 ); N = A.rows; LU = make_mat( N, N ); pivot = ( int* )malloc( sizeof( int ) * N ); z = ( double* )malloc( sizeof( double ) * N ); // A -> LU lu_decomp( A.dat, LU.dat, pivot, N, N ); // Ax = LUx = y Ux = z // Lz = y // Ux = z for( int row = 0; row < N; ++row) z[ row ] = y[ pivot[ row ] ] ; // y pivot for( int col = 0; col < row; ++col ) z[ row ] -= LU.dat[ pivot[ row ] ][ col ] * z[ col ]; for( int row = N-1; row >= 0; --row) x[ row ] = z[ row ]; for( int col = row +1; col < N; ++col ) x[ row ] -= LU.dat[ pivot[ row ] ][ col ] * x[ col ]; x[ row ] /= LU.dat[ pivot[ row ] ][ row ]; free_mat( LU ); free( pivot ); free( z ); 9
B main int main() int rows = 3; int cols = 2; // // // MATRIX A; A = make_mat( rows, cols ); //------------------------------------------------------- // :.dat[ ][ ] = A.dat[0][0] = 1; A.dat[0][1] = 2; A.dat[1][0] = 1; A.dat[1][1] = -3; A.dat[2][0] = -1; A.dat[2][1] = 2; //------------------------------------------------------- // printf( " A\n" ); print_mat( A ); //------------------------------------------------------- // B A printf( "\n \n" ); MATRIX B; B = make_mat( rows, cols ); copy_mat( A, B ); print_mat( B ); free_mat( B ); // B //------------------------------------------------------- // A 1,2,..,rows-1 1,2,...,cols-1 B printf( "\n \n" ); B = make_mat( rows-1, cols-1 ); copy_submat( A, B, 1, rows-1, 1, cols-1 ); print_mat( B ); free_mat( B ); //------------------------------------------------------- 10
// printf( "\n \n" ); B = make_mat( cols, rows ); // t_mat( A, B ); print_mat( B ); free_mat( B ); //------------------------------------------------------- // printf( "\n \n" ); MATRIX D; B = make_mat( rows, cols ); D = make_mat( rows, cols ); B.dat[0][0] = 2; B.dat[0][1] = 0; B.dat[1][0] = -1; B.dat[1][1] = 2; B.dat[2][0] = 0; B.dat[2][1] = 1; printf( "\na\n" ); print_mat( A ); printf( "\nb\n" ); print_mat( B ); plus_mat( A, B, D ); // D = A+B printf( "\na+b\n" ); print_mat( D ); minus_mat( A, B, D ); // D = A-B printf( "\na-b\n" ); print_mat( D ); printf( "\n \n" ); MATRIX C,E; C = make_mat( cols, rows ); // E = make_mat( rows, rows ); // C.dat[0][0] = -1; C.dat[0][1] = 0; C.dat[0][2] = 2; C.dat[1][0] = -1; 11
C.dat[1][1] = 2; C.dat[1][2] = 0; printf( "\na\n" ); print_mat( A ); printf( "\nc\n" ); print_mat( C ); prod_mat( A, C, E ); // E = A*C printf( "\na*c\n" ); print_mat( E ); free_mat( A ); free_mat( B ); free_mat( C ); free_mat( D ); free_mat( E ); //------------------------------------------------------- // printf( "\n \n" ); cols = 3; rows = 3; A = make_mat( rows, cols ); B = make_mat( rows, cols ); A.dat[0][0] = 1; A.dat[0][1] = 2; A.dat[0][2] = 1; A.dat[1][0] = 2; A.dat[1][1] = 3; A.dat[1][2] = 1; A.dat[2][0] = 1; A.dat[2][1] = 2; A.dat[2][2] = 0; inverse_mat( A, B ); printf( "\na\n" ); print_mat( A ); printf( "\na \n" ); print_mat( B ); printf( "\na * (A )\n" ); 12
C = make_mat( rows, cols ); prod_mat( A, B, C ); print_mat( C ); free_mat( A ); free_mat( B ); free_mat( C ); return 0; A 1.000 2.000 1.000-3.000-1.000 2.000 1.000 2.000 1.000-3.000-1.000 2.000-3.000 2.000 1.000 1.000-1.000 2.000-3.000 2.000 A 1.000 2.000 1.000-3.000-1.000 2.000 B 2.000 0.000-1.000 2.000 0.000 1.000 A+B 3.000 2.000 0.000-1.000-1.000 3.000 A-B -1.000 2.000 2.000-5.000 13
-1.000 1.000 A 1.000 2.000 1.000-3.000-1.000 2.000 C -1.000 0.000 2.000-1.000 2.000 0.000 A*C -3.000 4.000 2.000 2.000-6.000 2.000-1.000 4.000-2.000 A 1.000 2.000 1.000 2.000 3.000 1.000 1.000 2.000 0.000 A -2.000 2.000-1.000 1.000-1.000 1.000 1.000-0.000-1.000 A * (A ) 1.000 0.000 0.000 0.000 1.000 0.000 0.000 0.000 1.000 14