Matrix Operations C Program
Matrix Operations C Program
h>
Matrix* identity(long int a) { typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # int $ Matrix* mat%allocateMatrix(a&a) for(i%' i<%a() **i){ for(#%' #<%a() **#){ mat(>m+i,+#,%' " " for($%' $<%a() **$){ mat(>m+$,+$,%) " return mat " Matrix* ones(long int r& long int c){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # Matrix* mat%allocateMatrix(r&c) for(i%' i<%r() **i){ for(#%' #<%c() **#){ mat(>m+i,+#,%) " " return mat " Matrix* -eros(long int r& long int c){ typedef struct matrix Matrix struct matrix{
double** m long int ro!s long int columns " int i int # Matrix* mat%allocateMatrix(r&c) for(i%' i<%r() **i){ for(#%' #<%c() **#){ mat(>m+i,+#,%' " " return mat " Matrix* add(Matrix* a& Matrix* b){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # int r%a(>ro!s int c%a(>columns int r.%b(>ro!s int c.%b(>columns if((r/%r.)00(c/%c.)){ return(1233) " Matrix* $%allocateMatrix(r&c.) for(i%' i<%r() **i){ for(#%' #<%c() **#){ $(>m+i,+#,%(a(>m+i,+#,)*(b(>m+i,+#,) " "
return $ "
Matrix* smultiply(Matrix* matrix& long double scalar){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # int r%matrix(>ro!s int c%matrix(>columns
Matrix* 3%allocateMatrix(r&c) for(i%' i<%r() **i){ for(#%' #<%c() **#){ 3(>m+i,+#,%(scalar)*(matrix(>m+i,+#,) " " return 3 " Matrix* multiply(Matrix* a& Matrix* b){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int #&$&l double n%' int r%a(>ro!s int c%a(>columns int r.%b(>ro!s int c.%b(>columns if(c/%r.){ return(1233) " Matrix* f%allocateMatrix(r&c.) for(i%' i<%r() **i){ for(#%' #<%c.() **#){ for($%' $<%c() **$){ n%n*a(>m+i,+$,*b(>m+$,+#, " f(>m+i,+#,%n n%' " " return f " double trace(Matrix* matrix){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int r%matrix(>ro!s int c%matrix(>columns
double $%' if(c/%r){ return(') " int i int # for(i%' i<%c() **i){ $%$*matrix(>m+i,+i, " return $ " Matrix* augment(Matrix* a& Matrix* b){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # int l int m int r%a(>ro!s int c%a(>columns int r.%b(>ro!s int c.%b(>columns if(r/%r.){ return(1233) " Matrix* -%allocateMatrix(r&(c*c.)) for(i%' i<%r() **i){ for(#%' #<%c() **#){ -(>m+i,+#,%a(>m+i,+#, " " for(l%' l<%r.() **l){ for(m%' m<%c.() **m){ -(>m+l,+m*c,%b(>m+l,+m, " " return " Matrix* emultiply(Matrix* a& Matrix* b){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # int r%a(>ro!s
int c%a(>columns int r.%b(>ro!s int c.%b(>columns if((r/%r.)44(c/%c.)){ return(1233) " Matrix* $%allocateMatrix(r&c.) for(i%' i<%r() **i){ for(#%' #<%c() **#){ $(>m+i,+#,%(a(>m+i,+#,)*(b(>m+i,+#,) " " return $ " double norm(Matrix* matrix){typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # double t%' double !ea$%' long int r%matrix(>ro!s long int c%matrix(>columns for(i%' i<%r() **i){ for(#%' #<%c() **#){ t%t*po!(matrix(>m+i,+#,&.) " " t%po!(t&'.5) return t " Matrix* normali-e(Matrix* matrix){typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # double t%norm(matrix) double !ea$ long int r%matrix(>ro!s long int c%matrix(>columns Matrix* $%allocateMatrix(r&c)
for(i%' i<%r() **i){ for(#%' #<%c() **#){ $(>m+i,+#,%(matrix(>m+i,+#,)6t " " return $ " Matrix* upper(Matrix* matrix){ struct matrix{ double** m long int ro!s long int columns " int i%' int #%'&$%'&l%' int r%matrix(>ro!s int c%matrix(>columns if(c/%r){ return(') " Matrix* g%-eros(r&c) !hile(i<r){ g(>m+i,+i,%matrix(>m+i,+i, for($%' $<%i **$){ for(l%' l<%# **l){ g(>m+$,+i,%matrix(>m+$,+#, " " **# **i " return g " Matrix* lo!er(Matrix* matrix){ struct matrix{ double** m long int ro!s long int columns " int i%' int #%'&$%'&l%' int r%matrix(>ro!s int c%matrix(>columns if(c/%r){ return(') " Matrix* g%-eros(r&c) !hile(i<r){ g(>m+i,+i,%matrix(>m+i,+i,
for($%' $<%i **$){ for(l%' l<%# **l){ g(>m+i,+l,%matrix(>m+#,+l, " " **# **i " return g " Matrix* transpose(Matrix* matrix){ struct matrix{ double** m long int ro!s long int columns " int r%matrix(>ro!s int c%matrix(>columns Matrix* $%allocateMatrix(c&r) int i&# for(i%' i<%r() **i){ for(#%' #<%c() **#){ $(>m+#,+i,%matrix(>m+i,+#, " " return $ " int ise7ual(Matrix* a& Matrix * b){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # long int r%a(>ro!s long int c%a(>columns long int r.%b(>ro!s long int c.%b(>columns int $%' int 7%) if (r/%r.00c/%c.){ return $ " for(i%' i<%r() **i){ for(#%' #<%c() **#){ double n%a(>m+i,+#, double o% b(>m+i,+#,
"
Matrix* diagonal(Matrix* matrix){ typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # long int r%matrix(>ro!s long int c%matrix(>columns if(c/%r){ return(') " if(r/%c){return ' " Matrix* a % allocateMatrix(r&)) for(i%' i<%r() **i){ for(#%' #<%c() **#){ a(>m+i,+',%matrix(>m+i,+i, "" return a " Matrix* in8erse(Matrix* matrix){ 6* sorry sir&!e !ere not able to do the functions from here on!ards&so !e #ust made functions that !ould return some random 8alues so that !e can test our functions !ith the main.c file 9(*6 typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # long int r%matrix(>ro!s long int c%matrix(>columns srand(time(1233)) double p%rand()6rand() Matrix* 3%allocateMatrix(r&c) for(i%' i<%r() **i){ for(#%' #<%c() **#){ 3(>m+i,+#,%(matrix(>m+i,+#,)6p "
return 3
"
" long int ran$(Matrix* matrix){ 6* )5. :;2< =;>? @?<? for determining the ran$ of a matrix *6typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # long int r%matrix(>ro!s long int c%matrix(>columns srand(time(1233)) long int o%rand()Ar return o " double determinant(Matrix* matrix){ 6* )'. :;2< =;>? @?<? for computing the determinant of a matrix *6typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int # long int r%matrix(>ro!s long int c%matrix(>columns srand(time(1233)) double o%rand()Ar o%(o*c(r6B()*C)*CBD5EFG6rand() return o " Matrix* rref(Matrix* matrix){ 6* ).. :;2< =;>? @?<? for reducing matrices to its echelon form *6typedef struct matrix Matrix struct matrix{ double** m long int ro!s long int columns " int i int #
long int r%matrix(>ro!s long int c%matrix(>columns srand(time(1233)) double o%rand()Ar Matrix* h%copyMatrix(matrix) Matrix* n%smultiply(h&rand()6)5DB) return n "