summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2022-10-04 09:50:04 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2022-10-04 09:50:04 -0400
commitc956594b3b9349a982fe4721b31de922b6cacd88 (patch)
tree6c40cebdbbd6b7633e6e8265d597c96ff9470426
parent682d62632e727c2746f6443e69dca219ec24984c (diff)
operations sur des matrices
-rw-r--r--matAdd.c30
-rw-r--r--matMult.c43
2 files changed, 73 insertions, 0 deletions
diff --git a/matAdd.c b/matAdd.c
new file mode 100644
index 0000000..a6120c2
--- /dev/null
+++ b/matAdd.c
@@ -0,0 +1,30 @@
+/* matAdd.c
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <benjamin@chausse.xyz> wrote this file. As long as you retain this notice
+ * you can do whatever you want with this stuff. If we meet some day, and you
+ * think this stuff is worth it, you can buy me a beer in return.
+ * Benjamin Chausse
+ * ----------------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+#include "format.h"
+#include "testData.h"
+
+int *matAdd(int *ma, int *mb, int w, int h){
+
+ int res[h][w];
+ for (int i=0;i<w;i++){
+ for (int j=0;j<h;j++){
+ res[i][j] = *(ma+(i*h)+j) + *(mb+(i*h)+j);
+ }
+ }
+ return *res;
+}
+
+int main(){
+ int *test1 = matAdd(mtrxA,mtrxB,3,3);
+ print(test1,3,3);
+ return 0;
+}
diff --git a/matMult.c b/matMult.c
new file mode 100644
index 0000000..0de8daa
--- /dev/null
+++ b/matMult.c
@@ -0,0 +1,43 @@
+/* matAdd.c
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <benjamin@chausse.xyz> wrote this file. As long as you retain this notice
+ * you can do whatever you want with this stuff. If we meet some day, and you
+ * think this stuff is worth it, you can buy me a beer in return.
+ * Benjamin Chausse
+ * ----------------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+#include "format.h"
+#include "testData.h"
+
+int *matMult(int *ma, int *mb, int h1, int c, int w2){
+
+ int res[h1][w2];
+ for (int i=0;i<h1;i++){
+ for (int j=0;j<w2;j++){
+ int cell = 0;
+ for (int k=0;k<c;k++){
+ cell += *((ma+i*c)+k) * *((mb+w2*k)+j);
+ }
+ res[i][j] = cell;
+ }
+ }
+ return *res;
+}
+
+int main(){
+ int A[3][2] = {
+ {3, 4},
+ {7, 2},
+ {5, 9}
+ };
+ int B[2][3] = {
+ {3, 1, 5},
+ {6, 9, 7}
+ };
+ int *C = matMult(A,B,3,2,3);
+ print(C,3,3);
+ return 0;
+}