forked from OpenAI-VeniceAI-PMLL/pmll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_Matrix.c
101 lines (85 loc) · 2.73 KB
/
vector_Matrix.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
#include <stdio.h>
#include <stdlib.h>
// Define the Vector Matrix Structure
typedef struct {
int rows;
int cols;
int** data;
} vector_matrix_t;
// Function to Initialize a Vector Matrix
vector_matrix_t* init_vector_matrix(int rows, int cols) {
vector_matrix_t* matrix = (vector_matrix_t*)malloc(sizeof(vector_matrix_t));
if (!matrix) {
perror("Memory allocation failed for vector matrix");
exit(EXIT_FAILURE);
}
matrix->rows = rows;
matrix->cols = cols;
matrix->data = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
matrix->data[i] = (int*)malloc(cols * sizeof(int));
}
return matrix;
}
// Function to Free a Vector Matrix
void free_vector_matrix(vector_matrix_t* matrix) {
for (int i = 0; i < matrix->rows; i++) {
free(matrix->data[i]);
}
free(matrix->data);
free(matrix);
}
// Function to Populate the Vector Matrix with Values
void populate_vector_matrix(vector_matrix_t* matrix, int* values) {
int index = 0;
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols; j++) {
matrix->data[i][j] = values[index++];
}
}
}
// PMLL Logic Loop Integration
void pmll_vector_matrix_process(vector_matrix_t* matrix) {
printf("Processing the vector matrix...\n");
// Example: Matrix Multiplication (Square Matrix)
if (matrix->rows == matrix->cols) {
vector_matrix_t* result = init_vector_matrix(matrix->rows, matrix->cols);
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols; j++) {
result->data[i][j] = 0;
for (int k = 0; k < matrix->cols; k++) {
result->data[i][j] += matrix->data[i][k] * matrix->data[k][j];
}
}
}
printf("Resultant Matrix:\n");
for (int i = 0; i < result->rows; i++) {
for (int j = 0; j < result->cols; j++) {
printf("%d ", result->data[i][j]);
}
printf("\n");
}
free_vector_matrix(result);
} else {
printf("Non-square matrices are currently not supported for this operation.\n");
}
}
// Main Function
int main() {
// Example values for the matrix
int rows = 3;
int cols = 3;
int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
vector_matrix_t* matrix = init_vector_matrix(rows, cols);
populate_vector_matrix(matrix, values);
printf("Input Matrix:\n");
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols; j++) {
printf("%d ", matrix->data[i][j]);
}
printf("\n");
}
pmll_vector_matrix_process(matrix);
free_vector_matrix(matrix);
return 0;
}