## Element-by-Element Assignment to Vectors

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

### Element-by-Element Assignment to Vectors

The monadic function below assigns values to vector G on an element-by-element basis using values contained in vector x (the argument). x and G have the same rank, in this example, the rank is 5. Is there any way to avoid using line 3 below which defines G as a vector and assigns it rank 5? I know that vector G can be constructed by concatenation. However, given the length of the expressions that define elements of G, the element-by-element assignment is easier to write and modify. This code works fine but line 3 is more like FORTRAN or BASIC than APL.

Output←F2 x;G;K1;K2;K3;K4;K5;P;Pstd
⍝ Obtain rank of vector x and initially fill vector G with zeros
G←(⍴x)⍴0
K1←0.02497 ⋄ K2←0.01283 ⋄ K3←0.002062 ⋄ K4←0.002894 ⋄ K5←0.10894
P←0.1 ⋄ Pstd←0.1
G←K1-((2×x)*2)×(P÷Pstd)÷((2×x)+x-x)×(3+x+x+x+x+x)
G←K2-((2×x)*2)×(P÷Pstd)÷(x+x-x)×(3+x+x+x+x+x)
G←K3-(((2×x)+x-x)*2)×(x+x-x)×(P÷Pstd)÷ ((2-(2×x)+2×x)*2)×(3+x+x+x+x+x)
G←K4-(x+(2×x)-x)×((2×x)*2)×(P÷Pstd)÷((2-(2×x)+2×x)*2)×(3+x+x+x+x+x)
G←K5-((2×x)*2)×(x+x-x)×(P÷Pstd)÷((1-2×x)*2)×(3+x+x+x+x+x)
Output←G
REMINGTON30

Posts: 19
Joined: Fri Aug 11, 2017 2:17 pm

### Re: Element-by-Element Assignment to Vectors

You mean "same shape," rather than "same rank," right?

Putting aside the fact that you have some common subexpressions repeated and that if x has fewer than five elements, the function fails, it isn't clear what you do with expressions beyond G in the general case. So I'm guessing a bit here.

Below is a simple rewrite of your function, assuming just the five-element case. Perhaps there is something useful in it. By the way, for small vectors, I wouldn't worry about the distinction between setting indexed elements (G etc.) and then simply returning G versus setting named items and then catenating them into the result variable (e.g. G←G1 G2 G3 G4 G5). For large vectors, the latter is unwieldy.

`      (G1 G2 G3 G4 G5)←F2a x;K;P∆;sq;x∆;⎕IO ⎕IO←1 'F2a x: Argument "x" must have 5 or more items'⎕SIGNAL 11/⍨5>⍴x⍝ Obtain *SHAPE* of vector x and initially fill vector G with zeros⍝ Not sure what elements beyond G were used for... K←0.02497 0.01283 0.002062 0.002894 0.10894⍝ P←0.1 ⋄ Pstd←0.1 P∆←1                    ⍝ Are P and Pstd constants? x∆←3++/5↑x               ⍝ Only first five? sq←×⍨                  ⍝ Just to make the expressions BELOW easier to read.                        ⍝ I  restructured a tiny bit for perspicuity. G1←K-(sq 2×x)×P∆÷x∆×(2×x)+x-x G2←K-(sq 2×x)×P∆÷x∆×x+x-x G3←K-(sq(2×x)+x-x)×(x+x-x)×P∆÷x∆×sq 2-(2×x)+2×x G4←K-(x+(2×x)-x)×(sq 2×x)×P∆÷x∆×sq 2-(2×x)+2×x G5←K-(sq 2×x)×(x+x-x)×P∆÷x∆×sq 1-2×x`

I tested to make sure it gave the same results as yours using repeated calls to:
`      {(F2 ⍵)≡F2a ⍵}?5⍴0`
petermsiegel

Posts: 88
Joined: Thu Nov 11, 2010 11:04 pm

### Re: Element-by-Element Assignment to Vectors

Thank you Peter. I will incorporate most of your suggestions into my code. The formulation of the problem requires that x, G, and K always have the same number of elements. Since x is an input (argument), ⍴x provides the number of elements in both G and K. I like your re-write of the expressions defining G.

Incidentally, the code I provided is part of the solution for the equilibrium chemical composition of an ideal gas mixture. Elements of K are the equilibrium constants for 5 simultaneous chemical reactions, elements of G are the 5 non-linear algebraic equations that must be solved by a separate function to yield the 5 elements of x. The amounts of each chemical species present are computed algebraically from the vector x.
REMINGTON30

Posts: 19
Joined: Fri Aug 11, 2017 2:17 pm

### Re: Element-by-Element Assignment to Vectors

Good luck - and Happy New Year. Glad to see you are on your way to the solution you want.
petermsiegel

Posts: 88
Joined: Thu Nov 11, 2010 11:04 pm