EWISE Instruction |
EWISE array(I,J) = formula in I,J(RECTANGULAR, SYMMETRIC, PACKED arrays)
EWISE array(I) = formula in I(VECTOR arrays)
EWISE (short for Element–WISE) is a convenient method for setting all entries of an array. It is similar to the SET instruction for data series.
Sets element (I,J) or element (I) of array, for all I and J, according to the specified formula. Use COMPUTE for more standard matrix operations (such as inversion and multiplication).
Description
You must dimension the array before using EWISE. For each I or I,J combination within the bounds of the array, EWISE carries out the indicated calculation. The “function” can include multiple expressions, separated by commas—the values of the array will be set according to the last expression in the list. This can be useful when you need to do intermediate computations to produce the final values.
You can apply EWISE to arrays of any data type including arrays of arrays.
You can use any INTEGER variable in place of I and J. However, I and J are already defined as INTEGER's (primarily for use in EWISE); any other variable would need to be DECLARE'd.
Note that you can only use EWISE if you want to set all the entries of an array. An instruction like
ewise a(i,1)=....
(intending to set only column 1) is not permitted. Use a DO loop or the %DO function for this type of operation. For example:
do i=1,%rows(a)
compute a(i,1)=myseries1(i)
end do i
or
compute %do(i, 1, %rows(a), a(i,1)=myseries1(i))
Examples with Real-Valued Arrays
dec rect r(3,5)
ewise r(i,j)=j^(i-1)
dec rect r(3,3)
ewise r(i,j)=%if(i>=j,x(i-j+1),0.0)
The second example sets the elements of R below the diagonal to 0, while setting the elements on or above the diagonal to selected values out of X.
Examples with Other Arrays
You will generally use EWISE with real-valued arrays, since they form the basis for most calculations. You can, however, apply it to other types of arrays. In the next example, EWISE inverts 10 SYMMETRIC arrays which are stored in a VECTOR of SYMMETRIC arrays. The trickiest part about this is making sure that you get the correct arrays dimensioned before you do the EWISE. Here, it isn’t necessary to dimension the ten arrays stored in VSINV [VSINV(1),...,VSINV(10)], since each will get the dimension of the corresponding VS. It is VSINV itself that needs to be dimensioned.
dec vect[symm] vs(10) vsinv(10)
...Instructions setting VS(1),...,VS(10)
ewise vsinv(i) = inv(vs(i))
Notes
Anything that you can do with EWISE, you can also do with DO loops. If the DO loops would just loop over a COMPUTE instruction, use the more efficient EWISE. We recommend DO loops in several situations:
•The EWISE expression gets so complex (for instance, if it requires several nested %IF functions) that you cannot easily tell what it is doing.
•You can set up the DO loops to run over a reduced set of entries, where EWISE must set the entire array.
For instance, you might prefer to use
do i=1,%rows(a)
compute a(i,1)=b(i)
compute a(i,2)=c(i)
compute a(i,3)=d(i)
end do i
rather than
ewise a(i,j)=%if(j==1,b(i),%if(j==2,c(i),d(i)))
because the DO loop is significantly easier to read.
More Examples
dim a(r,r)
ewise a(i,j)=(j==i+1)
creates an r x r matrix with 1's on the first superdiagonal (that is, just above the diagonal where the column is one larger than the row).
if standardize {
dim lx(nvar)
ewise lx(j)=sqrt(%normsqr(%xcol(wx,j))/nper)
ewise wx(i,j)=wx(i,j)/lx(j)
}
first generates a vector with the mean square of each column of WX (%XCOL(wx,j) extracts column j, %NORMSQR returns the sum of squared elements). It then divides each element of WX by the LX for its column.
dim mu(nstates)
ewise mu(i) =%zeros(nvar,1)
MU is a VECTOR[VECTOR]. This dimensions it to have NSTATES elements, then initialize each of those component VECTORS to be an dimension NVAR zero vector.
dim %%responses(draw)(nvar*nvar,steps)
ewise %%responses(draw)(i,j)=ix=%vec(%xt(impulses,j)),ix(i)
This is a fairly complicated instruction which is used to "flatten" impulse responses. It sets the elements of %%RESPONSES(DRAW) (an element of a SERIES[RECT]). The matrix has NVAR*NVAR rows with STEPS columns. In the EWISE, the first step in the calculation is to extract the impulse response matrix for step J (using %XT applied to the RECT[SERIES] IMPULSES), and then flatten it into the VECTOR called IX using %VEC. This will be an NVAR*NVAR length VECTOR. The final calculation (which will give the value for element I,J) is to take element I of IX. So the J dimension is the response step and the I dimension packs responses from an NVAR x NVAR matrix to a NVAR*NVAR vector.
Copyright © 2025 Thomas A. Doan