How to add matrices with alglib? - c++

I already know how to multiply two matrices with alglib, using rmatrixgemm (see this question).
Is there a way to compute a linear combination of two matrices without using this function, setting B to the identity ? It wouldn't be very efficient.
Alglib provides tons of complex algorithms but I can't find such a basic function.
The manual is here.

I think rmatrixgencopy is ALGLIB's way of providing matrix addition when you set the alpha and beta input parameters both to 1.
rmatrixgencopy (C++)
rmatrixgencopy (C#)

you might be able just to use alglib.cmatrixgemm to do Addition.
This subroutine calculates C = alpha*op1(A)op2(B) +betaC where:
C is MxN general matrix
op1(A) is MxK matrix
op2(B) is KxN matrix
"op" may be identity transformation, transposition, conjugate transposition.
If you want to do C = A + C, you just need to set: B = Identity, alpha = 1, beta = 1, op = identity transformation.

Why don't you try using another library that was created for the purpose of matrix math such as MTL4?
http://www.simunova.com/en/node/24
Manual - http://www.simunova.com/node/148

Related

eigen: how to only compute the lower/upper part in the matrix inner product

I need to compute formula like "A'*A" using Eigen where A is an m by n matrix. The intuitive way to do this is,
result = A.transpose()*A;
But since the result is symmetric, is it possible to only compute the lower or upper part of result?
Yes, using selfadjointView and rankUpdate:
result.setZero();
result.selfadjointView<Lower>().rankUpdate(A.transpose());
This will only update the lower part of result.

What does MATLAB imresize() do if the input image matrix is actually of complex type?

After reading the imresize.m documentation, I don't understand what happens if the input image is actually a matrix of complex numbers. I am looking at some code that produces the necessary outputs and it uses imresize(A,n) where A is the complex matrix and n is the resize factor. I have successfully implemented a grayscale version (real double type) of imresize with bicubic interpolation in C++ but I have no idea how the function handles complex values.
Edit: More information
I have attempted to perform my currently working bicubic interpolation function to both the real and imaginary parts separately and then adding them together. It does not produce the same results.
My guess is that imresize works as if applied to real and imaginary part separately. Here is an example to demonstrate the effect:
N = 10;
scale = 2;
x = randn(N);
y = randn(N);
z1 = imresize(x+1i*y,scale);
z2 = imresize(x,scale)+1i*imresize(y,scale);
norm(z1-z2) % it is zero
In essence, it's just upsampling+filtering, so it would apply to real data in exactly the same w yas complex data.

OpenCV OCL logical indexing

I am working on an algorithm with many computations done on a GPU. I'm working mainly with oclMat structures and am trying to avoid copying from CPU to GPU and vice versa, yet I cannot find an easy way to:
compare all elements in an ocl matrix to a specific single value (be it float or double, for instance) and create a logical matrix in accordance
create an oclMat matrix with a given size and type initialized with all elements to a specific value (for example all elements are float and equal to 1.234567)
For example:
cv::ocl::oclMat M1 =...
// DO STUFF WITH M1
cv::ocl::oclMat logicalM1 = M1>1.55; // compare directly to a single value
cv::ocl::oclMat logicalM2 = ... ; // i.e. I want a 100x100 CV_32FC1 matrix with all elements set to be equal to 1.234567
By reading the documentation, it seems using cv::ocl::compare only works with both matrices the same dimensions and type, so maybe my first request isn't feasible. On the other hand, I don't know how to initialize a specific matrix directly in ocl (with cv::Mat I know how it's done).
I assume an easy workaround exists, but haven't found one yet... Thanks!
You are right. Looks like cv::ocl::compare supports only two cv::oclMat on input.
But you can create oclMat filled with specific value as follows:
cv::ocl::oclMat logicalM2(M1.size(), M1.type);
logicalM2.setTo(cv::Scalar(1.234567));
cv::ocl::oclMat logicalM1;
cv::ocl::compare(M1, logicalM2, logicalM1, cv::CMP_GT);
P.S. Also I suggest you trying new OpenCV 3.0 with Transparent-API which makes processing on GPU using OpenCL much easier.

Merge every member of cv::Mat A with cv::Mat B in order to create a cv::Mat C with [a(x,y),b(x,y)] as a result

I am trying to generate a cv::Mat C from two other matrices in order to get a third matrix that is made by 2-D points generated by combining 1-D points of matrices A and B.
My problem is that everything I tried only concatenates the matrices, and does not really merge each point with another, which would result in a 3-D matrix of 2-D points.
Does anyone can help me with this issue?
Thank you so much.
Just to be more clear:
What I have is A = [(0, 1, 2),(3, 4, 5)] and B=[(5, 4, 3),(2, 1, 0)] and I would like to create
C = {[(0,5), (1,4), (2,3)],
[(3,2), (4,1), (5,0)]}.
Thank you
You can do this by creating a custom type matrix. See here
Basically, you need to create a specialization of DataType class that handles points (pairs of data).
Or you can use the given matrices of std::complex<double> instead of integers as an ugly shortcut. Example code in the documentation:
Mat B = Mat_<std::complex<double> >(3, 3);
However, this solution might not be the real solution to your general problem. Google XY problem.

Armadillo - delete rows in a cube

I'm using Armadillo C++ library for matrices.
I have a 3d matrix (Cube) of size [73 256 1000].
I would like to take only part of the rows of the cube, e.g. in matlab
A = A(3:66, :, :);
How can I do it with Armadillo? I haven't found anything in the API.
Thanks.
The functionality is listed in Armadillo's documentation, in the subcube views section. You can use span::all to indicate an entire range. to For example:
cube A(100,50,10);
A.randu();
A = A(span(3,66), span::all, span::all);