0
$\begingroup$

I´m developing a code in c++ using openMP to test the power of parallelism using openMp. I create the code to solve linear system using Jacobi by aproximation. Now I need to test using a big matrix.

My matrix to test is little.

$$\begin{cases}2x_1-x_2 = 1\\ x_1+2x_2=3\end{cases}$$

Now I need help because I don´t know how to create a bigger matrix like this.

$\endgroup$

2 Answers 2

0
$\begingroup$

If all you need is a large positive definite matrix, take a random matrix $A$ and take $M=A^TA$. $M$ will then be symmetric positive semidefinite, since

  • $M^T = (A^TA)^T=A^T(A^T)^T=A^TA$
  • For any $x$, $x^TMx =x^TA^TAx = (Ax)^T Ax\geq 0$.

If the rank of $A$ is full, meaning $Ax=0$ implies $x=0$, then $M$ will also be positive definite.

$\endgroup$
6
  • $\begingroup$ Ok, I´m didn´t understand all, but my firs doubt is, and the values after equals? $\endgroup$ Feb 14, 2014 at 10:05
  • $\begingroup$ What I'm trying to say is that if you want a large positive definite matrix, take any matrix $A$ and calculate $A^TA$. The result will be positive (semi)definite. $\endgroup$
    – 5xum
    Feb 14, 2014 at 10:06
  • $\begingroup$ I understand that a I need to generate a random matrix, and multiply this matrix by his transposed. Correct? $\endgroup$ Feb 14, 2014 at 10:16
  • $\begingroup$ Yes. That way, you can get a positive definite $1000000\times 1000000$ matrix, if you want. $\endgroup$
    – 5xum
    Feb 14, 2014 at 10:17
  • $\begingroup$ Ok but I think that you don´t understand what´s my doubt...Imagine if I want a 2 x 2 matrix. It will be generated. a, b, c, d numbers, but it isn´t a expression like in my picture. because a + b = number, c + d = othernumber $\endgroup$ Feb 14, 2014 at 11:06
0
$\begingroup$

Well, on a $2\times 2$ system you won't see much improvements using any parallelism :-)

Since you implement the Jacobi method, I suppose you want your matrix to be sparse. Why don't you simply take, say, a finite difference discretization of a Laplacian? You can create it in MATLAB simply as follows:

% 1D Laplacian:
N  = 100; % Size of the matrix
A1 = gallery('tridiag', N); % Generate the tridiag(-1,2,-1) matrix of size N

% 2D Laplacian:
I  = speye(N); % Create the sparse NxN identity matrix
A2 = kron(I, A1) + kron(A1, I);

You can also create a 2D Laplacian by:

% 2D Laplacian, using gallery:
A2 = gallery('poisson', N);

Note that the size of $A_2$ is $N^2\times N^2$. Similarly, you can create a 3D discrete Laplacian ($N^3\times N^3$ matrix) by:

% 3D Laplacian:
A3 = kron(I,I,A1) + kron(I,A1,I) + kron(A1,I,I);

Now if you want to store a matrix, say, $A$, to a file, say, matrix.txt, you can do the following:

% Save A to a file
[i,j,a] = find(A);
matrix_data = [i,j,a];
save -ascii matrix.txt matrix_data

In the file, you find the triplets of the row and column indices and their associated matrix entry value on each line. You can read it simply in C++ and then do whatever you want to do with it.

$\endgroup$
10
  • $\begingroup$ I´ve never used MatLab. But I´m going to try your code. $\endgroup$ Feb 14, 2014 at 12:00
  • $\begingroup$ You can use Octave if you don't have access to Matlab, it's free. Except that Octave does not have the gallery command. Instead, you'd like to use the spdiags command to create the sparse tridiagonal matrix (A1 in the code above). You can do it like: A1=spdiags(ones(N,3)*diag([-1,2,-1]), -1:1, N, N). $\endgroup$ Feb 14, 2014 at 12:01
  • $\begingroup$ But it will generate a matrix like this for example? ax + b = c. Becausa I also need the c value. $\endgroup$ Feb 14, 2014 at 12:07
  • $\begingroup$ @DiegoMacario I don't understand at all your point. You said you wanted to generate a large SPD matrix (I still don't know if dense or sparse). If you want also the right hand side, then you can just pick any nonzero vector of the same size as the matrix (random, constant, etc.). $\endgroup$ Feb 14, 2014 at 12:10
  • $\begingroup$ My teache told me that I need to use a matrix positive defined, I got from a book the expression tested in my code. So I understand that a I need the c value by the example in the book. $\endgroup$ Feb 14, 2014 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .