openGPMP
Open Source Mathematics Package
mtx.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef MTX_HPP
35 #define MTX_HPP
36 
37 #include <cstdint>
38 #include <iostream>
39 #include <vector>
40 
41 #if defined(__GPMP_PYTHON_API__)
42 #define __PYTHON_API__
43 
44 #include <boost/bind/bind.hpp>
45 #include <boost/python.hpp>
46 #include <boost/python/numpy.hpp>
47 #endif
48 
49 namespace gpmp {
50 
51 namespace linalg {
52 
57 class Mtx {
58 
59  public:
60 #if defined(__GPMP_PYTHON_API__)
61 
62 // boost::python::numpy header
63 #include <boost/bind/bind.hpp>
64 #include <boost/python.hpp>
65 #include <boost/python/numpy.hpp>
66 
70  void mtx_add(const boost::python::numpy::ndarray &A,
71  const boost::python::numpy::ndarray &B,
72  boost::python::numpy::ndarray &C,
73  int rows,
74  int cols);
75 
76 // if Python API compilation is NOT occuring include these methods
77 #elif defined(__GPMP_CPP_API__)
78 
79 #define __GPMP_CPP__
84  void mtx_add_f90(float *A, float *B, float *C, std::size_t matrixSize);
85 
90  void mtx_add_f90(int *A, int *B, int *C, std::size_t matrixSize);
91 
92  void mtx_mult_f90(int *A,
93  int *B,
94  int *C,
95  std::size_t rows_a,
96  std::size_t cols_a,
97  std::size_t cols_b);
98 
99  /* void mtx_mult_f90(int *A,
100  int *B,
101  int *C,
102  std::size_t mtx_size);
103  */
104 #endif
105 
106 #if defined(__x86_64__) || defined(i386) || defined(__i386__) || \
107  defined(__i386) || defined(__amd64__) || defined(__amd64)
117  void
118  mtx_add(const int8_t *A, const int8_t *B, int8_t *C, int rows, int cols);
119 
129  void
130  mtx_add(const int16_t *A, const int16_t *B, int16_t *C, int rows, int cols);
140  void mtx_add(const int *A, const int *B, int *C, int rows, int cols);
141 
151  void
152  mtx_add(const double *A, const double *B, double *C, int rows, int cols);
153 
163  void mtx_add(const float *A, const float *B, float *C, int rows, int cols);
164 
174  void mtx_add(const std::vector<std::vector<int>> &A,
175  const std::vector<std::vector<int>> &B,
176  std::vector<std::vector<int>> &C);
177 
187  void mtx_add(const std::vector<std::vector<double>> &A,
188  const std::vector<std::vector<double>> &B,
189  std::vector<std::vector<double>> &C);
190 
200  void mtx_add(const std::vector<std::vector<float>> &A,
201  const std::vector<std::vector<float>> &B,
202  std::vector<std::vector<float>> &C);
203 
213  void
214  mtx_sub(const int8_t *A, const int8_t *B, int8_t *C, int rows, int cols);
215 
225  void
226  mtx_sub(const int16_t *A, const int16_t *B, int16_t *C, int rows, int cols);
236  void mtx_sub(const int *A, const int *B, int *C, int rows, int cols);
237 
247  void
248  mtx_sub(const double *A, const double *B, double *C, int rows, int cols);
249 
259  void mtx_sub(const float *A, const float *B, float *C, int rows, int cols);
260 
270  void mtx_mult(const int8_t *A,
271  const int8_t *B,
272  int8_t *C,
273  int rows_a,
274  int cols_a,
275  int cols_b);
285  void mtx_mult(const int16_t *A,
286  const int16_t *B,
287  int16_t *C,
288  int rows_a,
289  int cols_a,
290  int cols_b);
291 
301  void mtx_mult(const int *A,
302  const int *B,
303  int *C,
304  int rows_a,
305  int cols_a,
306  int cols_b);
307 
308  void mtx_mult(const int *A,
309  const int *B,
310  int64_t *C,
311  int rows_a,
312  int cols_a,
313  int cols_b);
314 
315  void mtx_mult(const double *A,
316  const double *B,
317  double *C,
318  int rows_a,
319  int cols_a,
320  int cols_b);
321 
322  void mtx_tpose(const int *A, int *C, int rows, int cols);
323 
333  void mtx_sub(const std::vector<std::vector<int>> &A,
334  const std::vector<std::vector<int>> &B,
335  std::vector<std::vector<int>> &C);
336 
346  void mtx_sub(const std::vector<std::vector<double>> &A,
347  const std::vector<std::vector<double>> &B,
348  std::vector<std::vector<double>> &C);
349 
359  void mtx_mult(const std::vector<std::vector<int>> &A,
360  const std::vector<std::vector<int>> &B,
361  std::vector<std::vector<int>> &C);
362 
372  void mtx_mult(const std::vector<std::vector<double>> &A,
373  const std::vector<std::vector<double>> &B,
374  std::vector<std::vector<double>> &C);
381  void mtx_tpose(std::vector<std::vector<int>> &matrix);
382 
389  void mtx_tpose(std::vector<std::vector<double>> &matrix);
390 
394 #elif defined(__ARM_ARCH_ISA_A64) || defined(__ARM_NEON) || \
395  defined(__ARM_ARCH) || defined(__aarch64__)
405  void mtx_add(const std::vector<std::vector<int>> &A,
406  const std::vector<std::vector<int>> &B,
407  std::vector<std::vector<int>> &C);
408 
418  void mtx_add(const std::vector<std::vector<float>> &A,
419  const std::vector<std::vector<float>> &B,
420  std::vector<std::vector<float>> &C);
421 
431  void mtx_sub(const std::vector<std::vector<int>> &A,
432  const std::vector<std::vector<int>> &B,
433  std::vector<std::vector<int>> &C);
434 
444  void mtx_sub(const std::vector<std::vector<float>> &A,
445  const std::vector<std::vector<float>> &B,
446  std::vector<std::vector<float>> &C);
447 
457  void mtx_mult(const std::vector<std::vector<int>> &A,
458  const std::vector<std::vector<int>> &B,
459  std::vector<std::vector<int>> &C);
460 
470  void mtx_mult(const std::vector<std::vector<double>> &A,
471  const std::vector<std::vector<double>> &B,
472  std::vector<std::vector<double>> &C);
473 
474  /*void mtx_mult_strass(const std::vector<std::vector<int>> &A,
475  const std::vector<std::vector<int>> &B,
476  std::vector<std::vector<int>> &C);
477 
478  void mtx_mult_strass(const std::vector<std::vector<double>> &A,
479  const std::vector<std::vector<double>> &B,
480  std::vector<std::vector<double>> &C);
481  */
488  void mtx_tpose(std::vector<std::vector<int>> &matrix);
489 
496  void mtx_tpose(std::vector<std::vector<double>> &matrix);
497 
498 #endif
499 
509  template <typename T>
510  void std_mtx_add(const T *A, const T *B, T *C, int rows, int cols) {
511  // MTX A AND B MUST BE SAME SIZE
512  for (int i = 0; i < rows; ++i) {
513  for (int j = 0; j < cols; ++j) {
514  // perform matrix addition
515  C[i * cols + j] = A[i * cols + j] + B[i * cols + j];
516  }
517  }
518  }
528  template <typename T>
529  void std_mtx_sub(const T *A, const T *B, T *C, int rows, int cols) {
530  // MTX A AND B MUST BE SAME SIZE
531  for (int i = 0; i < rows; ++i) {
532  for (int j = 0; j < cols; ++j) {
533  // perform matrix addition
534  C[i * cols + j] = A[i * cols + j] - B[i * cols + j];
535  }
536  }
537  }
538 
539  template <typename T, typename U>
540  void std_mtx_mult(const T *A,
541  const T *B,
542  U *C,
543  int rows_a,
544  int cols_a,
545  int cols_b) {
546  for (int i = 0; i < rows_a; ++i) {
547  for (int j = 0; j < cols_b; ++j) {
548  U sum = 0; // Use T type for sum
549  for (int k = 0; k < cols_a; ++k) {
550  sum += A[i * cols_a + k] * B[k * cols_b + j];
551  }
552  C[i * cols_b + j] = sum;
553  }
554  }
555  }
556 
557  template <typename T>
558  void std_mtx_tpose(const T *A, T *At, int rows, int cols) {
559  for (int i = 0; i < rows; ++i) {
560  for (int j = 0; j < cols; ++j) {
561  At[j * rows + i] = A[i * cols + j];
562  }
563  }
564  }
572  template <typename T>
573  void std_mtx_add(const std::vector<T> &A,
574  const std::vector<T> &B,
575  std::vector<T> &C);
576 
584  template <typename T>
585  void std_mtx_add(const std::vector<std::vector<T>> &A,
586  const std::vector<std::vector<T>> &B,
587  std::vector<std::vector<T>> &C);
588 
596  template <typename T>
597  void std_mtx_sub(const std::vector<std::vector<T>> &A,
598  const std::vector<std::vector<T>> &B,
599  std::vector<std::vector<T>> &C);
600 
608  template <typename T>
609  void std_mtx_mult(const std::vector<std::vector<T>> &A,
610  const std::vector<std::vector<T>> &B,
611  std::vector<std::vector<T>> &C);
612 
622  /*void std_mtx_mult_strass(const std::vector<std::vector<int>> &A,
623  const std::vector<std::vector<int>> &B,
624  std::vector<std::vector<int>> &C);
625  */
626 };
627 
628 } // namespace linalg
629 
630 } // namespace gpmp
631 
632 #endif
Mtx class offers matrix arithmetic operations. Some methods are element-wise while others make use of...
Definition: mtx.hpp:57
void std_mtx_mult(const T *A, const T *B, U *C, int rows_a, int cols_a, int cols_b)
Definition: mtx.hpp:540
void std_mtx_add(const T *A, const T *B, T *C, int rows, int cols)
Perform matrix addition on two matrices as flat arrays.
Definition: mtx.hpp:510
void std_mtx_sub(const T *A, const T *B, T *C, int rows, int cols)
Perform matrix subtraction on two matrices as flat arrays.
Definition: mtx.hpp:529
void std_mtx_tpose(const T *A, T *At, int rows, int cols)
Definition: mtx.hpp:558
void mtx_mult(std::vector< double > A, std::vector< double > B, std::vector< double > C)
The source C++ openGPMP namespace.
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23
constexpr int matrixSize
Definition: mtx.cpp:6
void mtx_add_f90()
Definition: mtx.cpp:333