How to create Eigen matrix out of 2 vectors [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 vectors and a matrix:
VectorXd A;
VectorXd B;
MatrixXd C;
How should I efficiently (without explicit loops and working fast)
compute matrix C values so that
C(i,k) = A(i) * B(k);
Assume that matrix C already has appropriate dimensions.
IMPORTANT: I only need help in using built-in Eigen syntax. Please no CUDA/MKL/BLAS suggestions.
Thank you.

You are looking for an outer product which is just a standard matrix product:
C = A * B.transpose();
Since the destination c does not alias with the operand of the product you can save one temporary with:
C.noalias() = A * B.transpose();
noalias makes sense for matrix products only.

Related

How to invert a matrix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to solve a system of n-linear equations with n-unknown variables in C++ using the gaussian method of elimnation. Any hints how to achieve that? I'll be probably using rand(); for the amount of n, since isn't available, because C++11 I can't use.
to solve a linear system
AX=B
you need to invert a matrix A, which results in A^(-1) and multiply A^(-1) * B to obtain X.
This is the example code to invert non-singular matrix using Gauss - Jordan elimination algorithm (complexity is O(n^3)):
matrix inversion using Gauss-Jordan elimination

get real part from fftw_complex in FFTW [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
this is maybe newbei question , but i dont know how to fast acces to real part of fftw_complex with FFFTW, i cant use .real() method,
I need convert this to double array, dynamic array in c++;
From the docs, 2 second Google search:
4.1.1 Complex numbers
The default FFTW interface uses double precision for all floating-point numbers, and defines a fftw_complex type to hold complex numbers as:
typedef double fftw_complex[2];
Here, the [0] element holds the real part and the 1 element holds the imaginary part.

Obtaining an algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have a variable a and b. Now I want to find a value for c such that the fraction a / c is a positive integer, and where c is as close to b as possible.
For example: a = 100 and b = 30.
In this case I want c to be 25; because a / c is an integer, and c is as close as b for which this holds.
Any ideas how I can program a function in C++ which does exactly this?
Find the factors of a. (search web for methods)
Scan resulting list for minimum difference vs b.
Is this a homework assignment? Either way, think about how you would solve this problem without writing any code. A good algorithm comes from a good design. Break the problem down into pieces and walk through some more examples. For example, how would you solve the problem of determining whether the division results in an integer value? Hint: There is a different operator you could use as opposed to division to achieve this easily. Now, how would you solve the problem of determining what number to start at for c in the algorithm? Do not write any code until you have the pseudocode figured out.

Nested/Recursive class in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am currently coding an algorithm that involving a recursive class. In Matlab, I can put a struct inside a struct, but I wonder if I can do the same thing in C++.
For example, I have a square matrix which can be divided into 4-smaller matrices. And each small matrix will be again decomposed into 4-smaller ones. The matrix will be divided until it has the pre-defined size. As the result, I can represent a matrix under a hierarchical tree.
In order words, I can say that each matrix has its own children and properties. Can you guide me an efficient way for programming this kind of problem. As the speed is very important for the algorithm, I am still seeking for a fastest way to implement the algorithm.
Thank in advance.
Kind Regards,
You can use pointers. Or containers.
struct Matrix
{
// some members
Matrix* child; //version 1
std::vector<Matrix> Children; //version 2
}
It can easily be done in C and C++ using pointers:
struct some_struct
{
int some_field;
double some_other_field;
struct some_struct *some_pointer_to_struct;
};
In C++ you can use normal standard container for this, if you want more than one:
struct some_struct
{
int some_field;
double some_other_field;
std::vector<some_struct*> collection_of_struct_pointers;
};
The important thing here is that you have to use pointers, as using the structure directly in itself (like struct some_struct foo;) doesn't work until the structure is fully defined, and it's not fully defined until the closing brace.

What's faster? (a+a vs 2*a and more) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In C/C++, I was wondering which is faster?
int a;
int b = a + a; // this
int b = 2 * a; // or this?
Also, is important the datatype? What about long? what about the number of times we add up?
(what about...)
long a;
long b = a + a + a +a;
long b = 4 *a;
Trust your optimizing compiler. It knows how to optimize for a specific CPU/architecture in ways that you will only be able to guess. Without reference to a specific architecture, there is no meaning to statements like "is x faster than y?", because it all depends on a huge number of factors.
And as always with performance questions, measuring is going to answer the question more completely than us offering semi-informed opinions and guesses.