0

Hello I am working on converting an encryption algorithm I wrote from java to C++, however I can't seem to figure out matrix multiplication in C++. I have the lapacke library, and am generating 2D arrays populated with doubles, as the matricies to preform operations on, but how do I preform the actual operations on my arrays.

Find below what I have so far:

#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include "include/lapacke.h"

using namespace std;

class construct {
public:

    string* load(string key) {
            string* kfb[] = split(key, "/");
            return kfb;
        }

        double** hologram(string blueprint) {
            string* baseArray = split(blueprint, ";");
            int size = *(&baseArray + 1) - baseArray;
            double** cluster = new double[size][10];
            for (int i = 0; i <= size - 1; i++) {
                string* temp = split(baseArray[i], ",");
                int tmpsize = *(temp + 1) - temp;
                for (int n = 0; n <= tmpsize - 1; n++) {
                    cluster[i][n] = (double) temp[n];
                }
            }
            int rows = sizeof cluster / sizeof cluster[0];
            int cols = sizeof cluster[0] / sizeof(cluster);
            return cluster;
        }

     vector<string> split(string str, string token) {
        vector<string>result;
        while (str.size()) {
            int index = str.find(token);
            if (index != string::npos) {
                result.push_back(str.substr(0, index));
                str = str.substr(index + token.size());
                if (str.size() == 0)result.push_back(str);
            }
            else {
                result.push_back(str);
                str = "";
            }
        }
        return result;
    }
}
  • Who knows? This depends on what operations you'd like to perform. Also, does this library have any documentation? I think it should have some. – ForceBru Jun 05 '18 at 11:48
  • 2
    I think you should take a few steps back, and [get a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) before continuing. There are so many errors in your code to make it seem like you're only guessing about things without real knowledge. – Some programmer dude Jun 05 '18 at 11:49
  • 1
    I don't see in your code where the multiplication is supposed to take place. Can you point it out. Without using a library like Eigen there is no actual "Matrix multiplication" in C/C++ or low level language. You need to do the scalar multiplication and addition over the rows/cols etc. From lin-alg, (A*B)[i,j] = A[i, k]*B[k, j] summed over all k. My notation is NOT C/C++ standard. –  Jun 05 '18 at 11:49
  • do yourself a favour and dont become a [three star programmer](http://wiki.c2.com/?ThreeStarProgrammer). Returning a pointer from `load` looks rather fishy. Also try to avoid c-casts (eg `(double)temp[n];`) like the plague, they are basically telling the compiler: "don't disturb me with your errors/warning, I know exactly what I am doing here." Why do you do that cast? – 463035818_is_not_an_ai Jun 05 '18 at 11:55
  • Forgive my ignorance, I just started coding in C++ less than 10 hours ago. @ggcg I have included a math library in my code, the lapacke math library, which is supposed to include matrix math functions. I'm not in that code specifically trying to preform the matrix math, I'm just looking for someone with knowledge about matrix math in C++ to show me how I should format my data and what functions I should call to actually take the arrays I've created as 2D arrays and multiply them. – Zachary Litzinger Jun 05 '18 at 12:04
  • 1
    If you just started less than a day ago, then you're definitely try to run before you can even stand up straight. Slow down, don't be hasty. C++ is a complex language and takes time to learn. Be patient and let it take the time it takes. – Some programmer dude Jun 05 '18 at 12:06
  • @user46, I am trying to return an array containing strings which will late be themselves loaded into arrays, I don't want to return a pointer to an array, I want to return an independent and unique array with load() the it's indexed content will be transformed into 2D arrays by hologram(). and I am casting strings to doubles because the way the keyfile generations works, there exist delimiters in the number data which seperates the data into an array of data that will later be turned into arrays on the fly. Though this is not the topic of this inquiry, I'm willing to take any pointers. – Zachary Litzinger Jun 05 '18 at 12:06
  • an array of strings would `std::vector` – 463035818_is_not_an_ai Jun 05 '18 at 12:07
  • Including math function is not the same as including overloaded operations. For C/C++ style arrays there is no overloaded matrix multiplication that I know of. I would write a loop and do a running sum over products as per my last comment. I use Eigen, which offers special matrix data objects and special version of multiplication * that will do all that under the hood, no need for loops. s/w languages do not automatically support this. You need to know your language. Eigen is not the only library that offers this but it's the one I know about. –  Jun 05 '18 at 12:21

0 Answers0