Last active
June 16, 2023 21:42
-
-
Save kiyoakii/7e41fcdf360f2d5ad6ec6e720d235084 to your computer and use it in GitHub Desktop.
Naive matrix multiplication program copied from somewhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import random | |
| random.seed(1234) | |
| def createRandomMatrix(n): | |
| maxVal = 1000 # I don't want to get Java / C++ into trouble | |
| matrix = [] | |
| for i in range(n): | |
| matrix.append([random.randint(0, maxVal) for el in range(n)]) | |
| return matrix | |
| def saveMatrix(matrixA, matrixB, filename): | |
| f = open(filename, "w") | |
| for i, matrix in enumerate([matrixA, matrixB]): | |
| if i != 0: | |
| f.write("\n") | |
| for line in matrix: | |
| f.write("\t".join(map(str, line)) + "\n") | |
| n = 1000 | |
| matrixA = createRandomMatrix(n) | |
| matrixB = createRandomMatrix(n) | |
| saveMatrix(matrixA, matrixB, "2000.in") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sstream> | |
| #include <string> | |
| #include <fstream> | |
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| struct Result { | |
| vector< vector<int> > A; | |
| vector< vector<int> > B; | |
| }; | |
| Result read(string filename) { | |
| vector< vector<int> > A, B; | |
| Result ab; | |
| string line; | |
| ifstream infile; | |
| infile.open (filename.c_str()); | |
| int i = 0; | |
| while (getline(infile, line) && !line.empty()) { | |
| istringstream iss(line); | |
| A.resize(A.size() + 1); | |
| int a, j = 0; | |
| while (iss >> a) { | |
| A.at(i).push_back(a); | |
| j++; | |
| } | |
| i++; | |
| } | |
| i = 0; | |
| while (getline(infile, line)) { | |
| istringstream iss(line); | |
| B.resize(B.size() + 1); | |
| int a; | |
| int j = 0; | |
| while (iss >> a) { | |
| B.at(i).push_back(a); | |
| j++; | |
| } | |
| i++; | |
| } | |
| infile.close(); | |
| ab.A = A; | |
| ab.B = B; | |
| return ab; | |
| } | |
| vector< vector<int> > ijkalgorithm(vector< vector<int> > A, | |
| vector< vector<int> > B) { | |
| int n = A.size(); | |
| // initialise C with 0s | |
| vector<int> tmp(n, 0); | |
| vector< vector<int> > C(n, tmp); | |
| for (int i = 0; i < n; i++) { | |
| for (int j = 0; j < n; j++) { | |
| for (int k = 0; k < n; k++) { | |
| C.at(i).at(j) += A.at(i).at(k) * B.at(k).at(j); | |
| } | |
| } | |
| } | |
| return C; | |
| } | |
| void printMatrix(vector< vector<int> > matrix) { | |
| vector< vector<int> >::iterator it; | |
| vector<int>::iterator inner; | |
| for (it=matrix.begin(); it != matrix.end(); it++) { | |
| for (inner = it->begin(); inner != it->end(); inner++) { | |
| cout << *inner; | |
| if(inner+1 != it->end()) { | |
| cout << "\t"; | |
| } | |
| } | |
| cout << endl; | |
| } | |
| } | |
| int main (int argc, char* argv[]) { | |
| string filename; | |
| if (argc < 3) { | |
| filename = "2000.in"; | |
| } else { | |
| filename = argv[2]; | |
| } | |
| Result result = read (filename); | |
| vector< vector<int> > C = ijkalgorithm(result.A, result.B); | |
| std::cout << "finished" << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment