aboutsummaryrefslogtreecommitdiffstats
path: root/buch/papers/multiplikation/code/helper_class.py
blob: 3b74f6738f3e7fba77c6da6bb04ccd56390d9b7e (plain)
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
102
103
104
105
106
#!/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,11,11,base=2,dtype=(np.int))
    # n=[8192]
    # C = helper.write_c_matrix(n)