Matrix multiplication using 1d arrays - c++

I'm trying to multiply two matrices stored inside 1d arrays.
I'm using this function, but my program crashes, I assume due to an out of bounds error.
However, I have no (easy) ability to debug, so I have to decide if my code is correct, and to me it seems it is...
void SampleUtils::multiplyMatrices(float* matA, int rA, int cA, float* matB,
int rB, int cB, float* matC, int rC, int cC) {
for (int i = 0; i <= rA; i++) {
for (int j = 0; j <= cB; j++) {
float sum = 0.0;
for (int k = 0; k <= rB; k++)
sum = sum + matA[i * cA + k] * matB[k * cB + j];
matC[i * cC + j] = sum;
}
}
So, can anyone find out what I did wrong?
Thanks...

Chances are you mean < instead of <= in your for loops.

Try to use i < rA , j < cB, k < rB in your for

Related

How to count sum of elements located in the upper left quarter of matrix

double CountSum(double **mat, int R, int C)
{
double sum = 0.0;
for(int i = 0; i < R / 2; i++)
{
for(int j = 0; j < C / 2; j++)
{
sum += mat[i][j];
}
}
return sum;
}
Am I correct do this, or where I have mistakes? Or if you have some piece of advice on how to pass parameters to function, please tell me about that
Assuming R and C are number of rows and number of columns respectively, this code won't work.
If R = 2 then (R - 1) / 2 = 0 so the outer loop won't be executed, because i < 0 is always false.
Don't subtract one, R / 2 would be enough. There are corner cases though, when R and C aren't even.
About parameters: you can add R and C to parameter list instead of i and j. (double **mat, int R, int C) and pass them respectively. From this current code, it looks like they are just global variables. i and j can be declared inside the function.
Code:
double CountSum(double **mat, int R, int C)
{
double sum = 0.0;
for(int i = 0; i < R / 2; i++)
{
for(int j = 0; j < C / 2; j++)
{
sum += mat[i][j];
}
}
return sum;
}
This is the working code, I hope you understand how to use it - pass it an appropriate arguments. R and C being height and width of the matrix or dimensions can be called as well. Note that if R or C or both are odd, then you only get the sum of the smaller part always, if you want the bigger part, you should ceil it, thus use (R + 1) / 2 instead of R / 2 and similar for C.

How to multiply a sparse matrix and a dense vector?

I am trying the following:
Eigen::SparseMatrix<double> bijection(2 * face_count, 2 * vert_count);
/* initialization */
Eigen::VectorXd toggles(2 * vert_count);
toggles.setOnes();
Eigen::SparseMatrix<double> deformed;
deformed = bijection * toggles;
Eigen is returning an error claiming:
error: static assertion failed: THE_EVAL_EVALTO_FUNCTION_SHOULD_NEVER_BE_CALLED_FOR_DENSE_OBJECTS
586 | EIGEN_STATIC_ASSERT((internal::is_same<Dest,void>::value),THE_EVAL_EVALTO_FUNCTION_SHOULD_NEVER_BE_CALLED_FOR_DENSE_OBJECTS);
According to the eigen documentaion
Sparse matrix and vector products are allowed. What am I doing wrong?
The problem is you have the wrong output type for the product.
The Eigen documentation states that the following type of multiplication is defined:
dv2 = sm1 * dv1;
Sparse matrix times dense vector equals dense vector.
If you actually do need a sparse representation, I think there is no better way of getting one than performing the multiplication as above and then converting the product to a sparse matrix with the sparseView member function. e.g.
Eigen::SparseMatrix<double> bijection(2 * face_count, 2 * vert_count);
/* initialization */
Eigen::VectorXd toggles(2 * vert_count);
toggles.setOnes();
Eigen::VectorXd deformedDense = bijection * toggles;
Eigen::SparseMatrix<double> deformedSparse = deformedDense.sparseView();
This can be faster than outputting to a dense vector if it is very sparse. Otherwise, 99/100 times the conventional product is faster.
void sparsem_densev_sparsev(const SparseMatrix<double>& A, const VectorX<double>& x, SparseVector<double>& Ax)
{
Ax.resize(x.size());
for (int j = 0; j < A.outerSize(); ++j)
{
if (A.outerIndexPtr()[j + 1] - A.outerIndexPtr()[j] > 0)
{
Ax.insertBack(j) = 0;
}
}
for (int j_idx = 0; j_idx < Ax.nonZeros(); j_idx++)
{
int j = Ax.innerIndexPtr()[j_idx];
for (int k = A.outerIndexPtr()[j]; k < A.outerIndexPtr()[j + 1]; ++k)
{
int i = A.innerIndexPtr()[k];
Ax.valuePtr()[j_idx] += A.valuePtr()[k] * x.coeff(i);
}
}
}
For a (probably not optimal) self-adjoint version (lower triangle), change the j_idx loop to:
for (int j_idx = 0; j_idx < Ax.nonZeros(); j_idx++)
{
int j = Ax.innerIndexPtr()[j_idx];
int i_idx = j_idx;//i>= j, trick to improve binary search
for (int k = A.outerIndexPtr()[j]; k < A.outerIndexPtr()[j + 1]; ++k)
{
int i = A.innerIndexPtr()[k];
Ax.valuePtr()[j_idx] += A.valuePtr()[k] * x.coeff(i);
if (i != j)
{
i_idx = std::distance(Ax.innerIndexPtr(), std::lower_bound(Ax.innerIndexPtr() + i_idx, Ax.innerIndexPtr() + Ax.nonZeros(), i));
Ax.valuePtr()[i_idx] += A.valuePtr()[k] * x.coeff(j);
}
}
}

Integer Division?

Regarding the code shown below:
#include <cmath>
int main()
{
const int n = 10000;
const int K = 10;
double* matrix = new double[n * n];
for(int k = 0; k < K; ++k) {
for(int j = 0; j < n; ++j) {
for(int i = 0; i < n; ++i) {
double ai = (double)i/double(n);
double aj = (double)i/double(n);
matrix[i * n + j] += pow(n, (double)k / K) / exp((double)k / K) * pow(sin(ai),2) * pow(sin(aj),2);
}
}
}
}
Is the way that lines
double ai = (double)i/double(n);
double aj = (double)i/double(n);
are defined is because we want floating-point division as opposed to integer division?
In addition, why are the operands cast in the way they are, i.e (double)i/double(n) instead of double(i)/double(n)?
Yes because if i and n are two integers as follow:
int i = ...;
int n = ...;
double ai = i/n
This will be an integer division. Say i=5 and n=9, even ai being a double it will end up with 0 as result.
You can either cast i or n to tell the compiler that you want a float division.
Yes. Actually, it would be enough to cast just one of the operands, but some programmers prefer to cast all the operands for consistency and more clarity.

I need to specify the size of an array dynamically

I have a Nx3 array which I need to fill as a function (so vector isn't an option). I already know how big N as as I feed it into the function as a parameter. I still get this stupid error of "must have a constant value", my code is:
double bspline_plot(double CP[], double Knot[], const int N, int d, int ncontrol, double *A){
// CP are the control points
//Knot is the knot vector
//N is the number of internal point you want in each segment
//d is the degree of the polynomials
double min_x, max_x, dx;
double *x_1;
x_1 = new double[N];
double A[N][2];
int i, j, M, L;
min_x = min(Knot);
max_x = max(Knot);
dx = (max_x - min_x) / N;
for (i = 0; i <= N; i = i + 1)
{
x_1[i] = min_x + dx*i;
}
M = ncontrol;
L = (sizeof(Knot) / sizeof(*Knot));
if (L < d + M + 1) // This checks if the number of control points are positive
{
printf("Incorrectly defined knot vector\n");
return;
}
else //This is the Cox - deBoor algorithm
{
for (i = 0; i <= N; i = i + 1)
{
for (j = 0; j <= L - 1; j = j + 1)
{
A[i][1] = A[i][1] + CP[j, 1] * CdB(j, d, x_1[i], Knot);
A[i][2] = A[i][2] + CP[j, 2] * CdB(j, d, x_1[i], Knot);
A[i][3] = A[i][3] + CP[j, 3] * CdB(j, d, x_1[i], Knot);
}
A[N][1] = CP[L, 2];
A[N][2] = CP[L, 2];
A[N][3] = CP[L, 1];
}
}
return A;
}
My other option is to feed in an array and then find it's values in the function but that seems a bit silly.
try to use std::vector in following way:
std::vector<std::vector<double>> A( N );
for( auto& row : A )
row.resize( M );
or
std::vector<std::vector<double>> A( N, std::vector<double>( M ));
From a quick inspection, the problem in your C++ code appears to be the following array declaration:
double A[N][2];
You need to dynamically allocate this 2d array as follows:
double** A = new double*[N];
for (int i=0; i<N; ++i)
A[i] = new double[2];
Have a look at this SO article for more information.
In the end I had to split A up into three vectors and change the output of the function from double to void and read in the (now) three vectors as double*. I can then just change the contents of the vectors and it now is showing no errors.

C++ Returning double** from matrix

Hi everyone I got a trouble with the returning element of a function. I need to return a double pointer to pointer "double**". But I got a double[][] matrix.
Here is the code:
double** createPalette(int r, int g, int b) {
double incR = 1 / r, incG = 1 / g, incB = 1 / b;
double Cp[r * g * b][3];
for (int i = 0; i < r; i++) {
for (int j = 0; j < g; j++) {
for (int k = 0; k < b; k++) {
Cp[i * r + j * g + k][0] = incR * i;
Cp[i * r + j * g + k][1] = incG * j;
Cp[i * r + j * g + k][2] = incB * k;
}
}
}
return Cp; //return &cp... (?)
}
I was looking for on internet, but I only found about simple pointer, no pointer to pointers.What should I do?
Thanks for all.
I think you know the value of r,g,b,so you can get the size of the matrix,you can do like this
void createPalette(int r, int g, int b, double matrix[][3])