I have a huge m by 1 array (m is very large) called X which is a result of Fortran matmul operation. My problem is to store this apparently 2D array into an 1D array Y of size m.
I tried with Y = reshape(X, [[2]]) and this result some elements NaN. Can anyone point me to Fortran commands to do it quickly. The elements of X may be zero or non-zero.
The second argument of reshape (or the one with keyword shape=) is the shape of the function's result. In your call, you have requested shape [2].
An array with shape [2] is a rank-1 array with two elements. You want a rank-1 array with m elements:
Y = RESHAPE(X, [m])
Now, in this case there's no need to use reshape:
Y = X(:,1)
where the right-hand side is the rank-1 array section of X.
When you have Y=reshape(X,[2]), if Y is not allocatable and not of size 2 then you have a problem which may indeed result in your compiler deciding---as it is quite entitled to do---to give you a few NaNs.
Note also that you may not need to reshape your array, depending on how you intend to later use it.
Related
I've never written in Fortran, but I'm trying to adapt a script to R and the following lines are confusing me. So this is how the variable is defined:
real, dimension(n,nd) :: x
Does this mean x is n arrays filled with nd number of real values or a n x nd matrix?
Then
amax = maxval(abs(x))
x = x/amax
is applied. Is the variable amax a global max of the absolute values in x or is it an array of n max values, one for each row? This is important to know if the x = x/amax is being applied to each row or the entire matrix. The purpose of this function seems to be some type of normalization.
The question of the title is much more general than that of the body, so I'll come to that later.
The result of maxval(array) is a scalar, being the maximum value in array (if it's of non-zero size).
In your example, x is a single array of rank 2 (which is commonly thought of as being a matrix). Thus, maxval(x) is indeed what you call the global maximum of that matrix. An alternative form of maxval is required to give the row-by-row maxima: maxval(x,dim=2).
Now, there is something else to note from your example:
x = x/amax
has a requirement about the shapes of x and amax.
You don't give a declaration for amax but there are two possibilities:
amax has the same shape as x; or
amax is a scalar.
[Note that amax needn't be a scalar just because it is assigned a scalar result from that maxval reference. However, you will see that amax won't be declared as rank 1 with size the number of rows of x, so that's another clue that maxval is giving the global maximum.]
These two possibilities come from conformability rules for division. With amax a scalar each element of x is divided by that value; with amax an array each element of x is divided by the corresponding element in amax.
If you want to normalize each individual row of x then you just can't use that division expression with amax a rank 1 array.
Coming to the more general question: even though it's an either/or question the answer is "no". There is no single way. Each function acts as it is defined.
As a general rule, though, the intrinsic functions of Fortran rarely care about the specific case of arrays which have "rows". But one useful thought is that a function acts either:
on all elements individually, returning an array of the same shape;
on the array as a whole, returning a scalar.
Moderated by the fact that many will have this dim argument which causes the function to act on slices instead.
The first line means that the variable x is an array of two dimensions (n,nd) and not n arrays of nd values. The function maxval returns the maximum value in this array.
See page 130 (in the PDF not the printed number) in F90_notes.pdf (you will also find a whole chapter concerning the arrays in the same document).
To add to Baruchel's answer: x/amax divides each element of the 2D array x by the scalar amax.
I am using m.conservativeResize() to do the equivalent in Eigen as the reshape function in MatLab. So let N = 3, and then...
static MatrixXd m(N*N,1);
and then I assign this matrix some values, and it looks like this:
1
1
0
1
0
1
0
1
1
and then try to reshape it...
m.conservativeResize(N,N);
So now the same values should be there, but now in N rows and N columns rather than N*N rows and one column.
However that's not what I get. The first column has the first three values in the column vector - OK so far - but then the remaining values just look like garbage values from uninitialized memory:
1 3.08116e-309 0.420085
1 -2.68156e+154 1.2461e-47
0 -2.68156e+154 0.634626
Any idea what I am doing wrong?
conservativeResize() doesn't ``move" the elements around (in other words, doesn't work like MATLABs reshape, since it performs memory re-allocation even if the initial and final sizes are the same). From the documentation:
Resizes the matrix to rows x cols while leaving old values untouched.
...
Matrices are resized relative to the top-left element. In case values need to be appended to the matrix they will be uninitialized.
These statements seem a bit confusing. What it means is the following: think about the initial matrix as a rectangle, of size A x B. Then think about the resized matrix as another rectangle of size C x D. Then mentally overlap the two rectangles, making sure the top-left corner is common to both. The common elements of the intersection are the ones that are preserved by the conservativeResize. The rest just correspond to uninitialized memory.
In case you want a true reshaping, use resize() instead (making absolutely sure that A x B == C x D, otherwise reallocation takes place and all bets are off).
I have a 3x2 array and have filled it with the numbers 1-6
so that it looks like
1 4
2 5
3 6
I then call maxval on it, and specify that I wish to find the max value along dimension 1. One would expect that it should return 3, no?
But for some reason my output is '3 6'
PROGRAM maxv
IMPLICIT None
INTEGER, DIMENSION(3,2) :: x
DATA x /1,2,3,4,5,6/
WRITE(*,*) maxval(x,dim=1)
ENDPROGRAM maxv
I used Gfortran 4.6.3 if the issue lies within my compiler
According to http://www.nsc.liu.se/~boein/f77to90/a5.html , maxval when you specify a dimension is supposed to supply the maxval in that dimension.
Or maybe I have overlooked some stuffs.
Yes, you overlooked some stuffs; maxval is behaving correctly.
When you write, for a rank-2 array x
maxval(x,dim=1)
the function returns a rank-1 array with the same number of elements as there are columns in x, each element being the maximum value of the corresponding column in x. Similarly
maxval(x,dim=2)
would, for your example, return the rank-1 array [4,5,6] -- the maximum value in each row of x.
The GNU documentation explains the function better than the source you cite, IBM explain it even better and include an example of the function's use.
this is a very simple question but whoever i ask it they cant answer it.
in two dimensional arrays we have to specify rows and columns,right?
for example:
void foo( int towers **[ ][ 3 ]** , int rings )
{
for (int ring = 0; ring < rings; ring++)
{
... towers[ ring ][ 0 ]...
}
}
in programming its okay if we dont write(mention) rows as you see in that piece of code,but we ALWYAS have to write number of columns no matter what.
so what is the logic reason that why we should write number of columns? what will happen if we dont?
Whenever you try to access a two dimensional array, you specify the two indices: array[a][b]. C++ ensures all the elements in array[n][...] are contiguous in memory for any n, then the next bit of memory is used for array[n+1][...] etc.. For example:
array[0][0] ((T*)array)
array[0][1] ((T*)array) + 1
array[0][2] ((T*)array) + 2
array[1][0] ((T*)array) + 3
array[1][1] ((T*)array) + 4
...
So, the absolute memory address of array[a][b], in an array T array[A][B] is:
((T*)array) + a * B + b;
See how the calculation needs B but not A? Similarly, the compiler is insisting you provide all but the left-most index.
In some ways, it would be nice if the compiler verified that you didn't try to index to a value of [a] at or past A, but the language makes no such checks - it's up to the programmer to ensure their code indexes the array safely. If you want safety you can use a std::vector<> and at() for runtime index checking, or even write a fixed-size-array mechanism that checks compile-time-constant indices at compile time (not very useful, as indices typically vary at runtime).
You know how two dimensional array is stored in the memory. If it is of size [n][m] then it just takes n x m consecutive cells in the memory. Afterwords when you get asked about element [a][b] there is the calculation that takes the element a * m + b from these consecutive cells. However this calculation can not be performed if you do not supply the number of columns (because otherwise the m will not be known).
On the other hand the number of columns and rows is not stored in the memory portion for the array and the number of columns can not be derived from the memory either. All this means that the computer will not know which memory address to serve you.
in C++, what is the indexing value for a W * H * D sized 3D array?
for a particular i, j, k is this the correct indexing:
i*W*H+j*W+k
What you have written is equivalent to the pointer arithmetic that this would do:
T x[D][H][W];
x[i][j][k]; // Pointer arithmetic done here
Obviously, depending on how you order D, H and W (or i, j, k), the calculation will differ.
There is no one "correct" order, but the version you've given should work. The order in which you apply the indices will determine whether you do row-major or column-major indexing. If you're porting Fortran code (for example) it can make sense to reverse the "normal" C order.
Width, height and depth are meaningless in this context. What you need to know is that multidimensional arrays are stored in row-major order.
Yes, assuming i varies from 0 ... D-1, j varies from 0 ... H-1, and k varies from 0 ... W-1.
Usually, though, the purpose of having an indexer, I thought, was to express relations within a sparse matrix so you didn't need to deal with the whole thing (and expend memory for it). If your data span the whole matrix, you might look into creating the 3d matrix as a pointer to an array of pointers, which themselves each point to an array of pointers. Using this allows you to use the x[i][j][k] notation but may be faster.
See http://www.nr.com/cpppages/chapappsel.pdf for a description.
If you need to to iterarate over all elements it is best to do in
for i
for j
for k
order. This way, it would be fastest, because index of array is incremented by one each time and values could be precached.
There is no only one correct way to do this but you probably chose best one.