forked from swarupgt/Matrix-operations-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixIO.c
106 lines (90 loc) · 2.13 KB
/
MatrixIO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include "MatrixLibrary.h"
#define Rand_upper 20
void initialiseZero(Matrix *a, int row, int column)
{
a->r = row;
a->c = column;
//First making the rows (basically an arrray of float pointers)
a->MatPtr = calloc(a->r, sizeof(float *));
//Adding elements to each row (0)
for (int i = 0; i < a->r; i++) a->MatPtr[i] = calloc(a->c, sizeof(float));
}
void initialiseOne(Matrix *a, int row, int column)
{
a->r = row;
a->c = column;
//First making the rows (basically an arrray of float pointers)
a->MatPtr = calloc(a->r, sizeof(float *));
for (int i = 0; i < a->r; i++) {
//Adding elements to each row (0)
a->MatPtr[i] = calloc(a->c, sizeof(float));
//Making the elements 1.
for (int j = 0; j < a->c; j++) a->MatPtr[i][j] = 1;
}
}
void initialiseRandom(Matrix *a, int row, int column)
{
a->r = row;
a->c = column;
//First making the rows (basically an arrray of float pointers)
a->MatPtr = calloc(a->r, sizeof(float *));
for (int i = 0; i < a->r; i++) {
//Adding elements to each row (0)
a->MatPtr[i] = calloc(a->c, sizeof(float));
//Assigning random numbers
for (int j = 0; j < a->c; j++) a->MatPtr[i][j] = rand()%Rand_upper;
}
//Include #include <time.h>
// #define Random_upper ()
// srand(time(NULL)) //In in the main function before calling the init_random function.
}
void printMatrix(Matrix n)
{
for (int i = 0; i < n.r; i++)
{
for (int j = 0; j < n.c; j++)
{
printf("%.3f ", n.MatPtr[i][j]);
}
printf("\n");
}
}
void readMatrix(Matrix a)
{
for (int i = 0; i < a.r; i++)
{
for (int j = 0; j < a.c; j++)
{
scanf("%f", &a.MatPtr[i][j]);
}
}
}
Matrix identity(int n)
{
Matrix iden;
initialiseZero(&iden, n, n);
for(int i=0;i<n;i++)
{
iden.MatPtr[i][i] = 1;
}
return iden;
}
Matrix vector(float a, float b, float c)
{
Matrix mat;
initialiseZero(&mat, 3, 1);
mat.MatPtr[0][0] = a;
mat.MatPtr[1][0] = b;
mat.MatPtr[2][0] = c;
return mat;
}
void delMatrix(Matrix A)
{
for(int i = 0; i < A.r; i++)
free(A.MatPtr[i]);
free(A.MatPtr);
}