diff --git a/build/libtensor.so b/build/libtensor.so index 7d6f264..372e753 100755 Binary files a/build/libtensor.so and b/build/libtensor.so differ diff --git a/csrc/tensor.cpp b/csrc/tensor.cpp index 1a283ec..ac11b55 100644 --- a/csrc/tensor.cpp +++ b/csrc/tensor.cpp @@ -45,14 +45,14 @@ extern "C" { } printf("]\n"); - printf("Data:\n["); + /*printf("Data:\n["); for (int i = 0; i < stride; i++) { printf("%.2f", tensor->data[i]); if (i < stride - 1) { printf(", "); } } - printf("]\n\n\n"); + printf("]\n\n\n");*/ return tensor; } @@ -81,24 +81,24 @@ extern "C" { } printf("Size: %d\n", tensor1->size); - printf("Data: ["); + /*printf("Data: ["); for (int i = 0; i < tensor1->size; i++) { printf("%.2f", tensor1->data[i]); if (i < tensor1->size - 1) { printf(", "); } } - printf("]\n"); + printf("]\n");*/ printf("Size: %d\n", tensor2->size); - printf("Data: ["); + /*printf("Data: ["); for (int i = 0; i < tensor2->size; i++) { printf("%.2f", tensor2->data[i]); if (i < tensor2->size - 1) { printf(", "); } } - printf("]\n"); + printf("]\n");*/ printf("Shapes : ["); for (int i = 0; i < tensor1->ndim; i++) { diff --git a/csrc/tensor.h b/csrc/tensor.h index 08052cb..18d1066 100644 --- a/csrc/tensor.h +++ b/csrc/tensor.h @@ -17,6 +17,7 @@ extern "C" { Tensor* sub_tensor(Tensor* tensor1, Tensor* tensor2); Tensor* elementwise_mul_tensor(Tensor* tensor1, Tensor* tensor2); void reshape_tensor(Tensor* tensor, int* new_shape, int new_ndim); + Tensor* matmul_tensor(Tensor* tensor1, Tensor* tensor2); } #endif /* TENSOR_H */ diff --git a/csrc/tensor.o b/csrc/tensor.o index e5df588..c1edeeb 100644 Binary files a/csrc/tensor.o and b/csrc/tensor.o differ diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 8858bf5..39b3449 100644 Binary files a/norch/__pycache__/tensor.cpython-38.pyc and b/norch/__pycache__/tensor.cpython-38.pyc differ diff --git a/norch/tensor.py b/norch/tensor.py index 2b0c40c..2b95601 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -177,21 +177,64 @@ class Tensor: return result_data + +def matrix_sum(matrix1, matrix2): + # Check if the matrices can be multiplied + if len(matrix1[0]) != len(matrix2): + raise ValueError("Matrices cannot be multiplied. Inner dimensions must match.") + + # Initialize the result matrix with zeros + result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))] + + # Perform matrix multiplication + for i in range(len(matrix1)): + for j in range(len(matrix2[0])): + result[i][j] += matrix1[i][i] + matrix2[i][j] + + return result + + if __name__ == "__main__": from tensor import Tensor import time + import random + import numpy as np - ini = time.time() - a = Tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],[[13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]]]) - print(a) - b = a.reshape([4, 3, 2]) + + #a = Tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],[[13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]]]) + #print(a) + N = 1000 + a = Tensor([[random.uniform(0, 1) for _ in range(N)] for _ in range(N)]) + b = Tensor([[random.uniform(0, 1) for _ in range(N)] for _ in range(N)]) #b = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) #a = Tensor([[[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]]]) #b = Tensor([[[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]]]) - #c = a @ b + ini = time.time() + c = a + b - print("\n###########", b) + print("\n#####2######") fim = time.time() - print(fim-ini) \ No newline at end of file + print(fim-ini) + + print("\n\n") + + + a = [[random.uniform(0, 1) for _ in range(N)] for _ in range(N)] + b = [[random.uniform(0, 1) for _ in range(N)] for _ in range(N)] + ini = time.time() + result_matrix = matrix_sum(a, b) + fim = time.time() + print(fim-ini) + + + print("\n\n") + + ini = time.time() + a = np.random.rand(N, N) + b = np.random.rand(N, N) + result_matrix = a + b + fim = time.time() + print(fim-ini) +