AlgoPlus v0.1.0
Loading...
Searching...
No Matches
multiply.h
1#pragma once
2
3#ifdef __cplusplus
4#include <iostream>
5#include <vector>
6#endif
7
8/*
9 * @brief mul: multiplies two 2D vectors
10 * @brief x: The first passed 2D vector
11 * @brief y: the second passed 2D vector
12 * @return: A 2d vector with x.size() x y[0].size() dimensions
13 */
14template <typename T>
15std::vector<std::vector<T>> multiply(std::vector<std::vector<T>> const& x,
16 std::vector<std::vector<T>> const& y) {
17 assert(x[0].size() == y.size());
18 std::vector<std::vector<T>> out(x.size(), std::vector<T>(y[0].size()));
19
20 for (size_t i = 0; i < x.size(); i++) {
21 for (size_t j = 0; j < y[0].size(); j++) {
22 for (size_t k = 0; k < x[0].size(); k++) {
23 out[i][j] = x[i][k] * y[k][j];
24 }
25 }
26 }
27
28 return out;
29}
30
31template <typename T>
32double multiply(std::vector<T> const& x, std::vector<T> const& y) {
33 assert(x.size() == y.size());
34 double out = 0.0;
35 for (size_t i = 0; i < x.size(); i++) {
36 out += x[i] * y[i];
37 }
38
39 return out;
40}