diff options
author | Nao Pross <np@0hm.ch> | 2021-07-31 17:30:44 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2021-07-31 17:30:44 +0200 |
commit | d7723ea05fe173c5203961dbc74aadf548338fb0 (patch) | |
tree | 12247fbfe674bc7b0f364c19b265ce8bb1059bb3 /buch/papers/multiplikation/code/helper_class.py | |
parent | Fix commas and details in references.bib (diff) | |
parent | Merge pull request #58 from Kuehnee/master (diff) | |
download | SeminarMatrizen-d7723ea05fe173c5203961dbc74aadf548338fb0.tar.gz SeminarMatrizen-d7723ea05fe173c5203961dbc74aadf548338fb0.zip |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'buch/papers/multiplikation/code/helper_class.py')
-rwxr-xr-x | buch/papers/multiplikation/code/helper_class.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/buch/papers/multiplikation/code/helper_class.py b/buch/papers/multiplikation/code/helper_class.py new file mode 100755 index 0000000..485fa76 --- /dev/null +++ b/buch/papers/multiplikation/code/helper_class.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Fri Mar 12 09:02:48 2021 + +@author: nunigan +""" + +from datetime import datetime +import numpy as np + +class Helper(): + def __init__(self): + pass + + def write_c_matrix(self, n_array): + + with open('c_matrix.h', 'w') as file: + file.writelines('/* Seminar Matrizen, autogenerated File, Michael Schmid, {} */ \n \n'.format(datetime.now().strftime("%d/%m/%Y, %H:%M:%S"))) + + file.writelines('#include <stdint.h> \n') + + + + for k, n in enumerate(n_array): + A = np.random.randint(-100,100,(n,n)) + B = np.random.randint(-100,100,(n,n)) + file.writelines('const int A{}[][{}] = \n'.format(k, n)) + file.writelines(' {\n') + for i in range(n): + file.writelines(' {') + for j in range(n): + if j == n-1: + file.writelines('{}'.format(A[i,j])) + else: + file.writelines('{},'.format(A[i,j])) + if i == n-1: + file.writelines('}\n') + else: + file.writelines('},\n') + + file.writelines(' };\n') + + file.writelines('const int B{}[][{}] = \n'.format(k,n)) + file.writelines(' {\n') + for i in range(n): + file.writelines(' {') + for j in range(n): + if j == n-1: + file.writelines('{}'.format(B[i,j])) + else: + file.writelines('{},'.format(B[i,j])) + if i == n-1: + file.writelines('}\n') + else: + file.writelines('},\n') + + file.writelines(' };\n') + + file.writelines('const double dB{}[][{}] = \n'.format(k,n)) + file.writelines(' {\n') + for i in range(n): + file.writelines(' {') + for j in range(n): + if j == n-1: + file.writelines('{}'.format(B[i,j])) + else: + file.writelines('{},'.format(B[i,j])) + if i == n-1: + file.writelines('}\n') + else: + file.writelines('},\n') + + file.writelines(' };\n') + + file.writelines('const double dA{}[][{}] = \n'.format(k,n)) + file.writelines(' {\n') + for i in range(n): + file.writelines(' {') + for j in range(n): + if j == n-1: + file.writelines('{}'.format(A[i,j])) + else: + file.writelines('{},'.format(A[i,j])) + if i == n-1: + file.writelines('}\n') + else: + file.writelines('},\n') + + file.writelines(' };\n') + + file.writelines('const int *Ap[{}] = {{{}}}; \n'.format(len(n_array),",".join(['(int*) A'+str(element) for element in np.arange(len(n_array))]))) + file.writelines('const int *Bp[{}] = {{{}}}; \n'.format(len(n_array),",".join(['(int*) B'+str(element) for element in np.arange(len(n_array))]))) + file.writelines('const double *dAp[{}] = {{{}}}; \n'.format(len(n_array),",".join(['(double*) dA'+str(element) for element in np.arange(len(n_array))]))) + file.writelines('const double *dBp[{}] = {{{}}}; \n'.format(len(n_array),",".join(['(double*) dB'+str(element) for element in np.arange(len(n_array))]))) + file.writelines('int n[{}] = {{{}}}; \n'.format(len(n_array),",".join([str(element) for element in n_array]))) + file.writelines('int n_arrays = {};\n'.format(len(n_array))) + +# test%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +if __name__ == '__main__': + + helper = Helper() + # n = np.arange(2,10) + n = np.logspace(1,3,3,base=2,dtype=(np.int)) + C = helper.write_c_matrix(n) |