Element-by-Element Assignment to Vectors

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

Element-by-Element Assignment to Vectors

Postby REMINGTON30 on Tue Dec 26, 2017 8:22 pm

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[1]←K1-((2×x[1])*2)×(P÷Pstd)÷((2×x[3])+x[4]-x[1])×(3+x[1]+x[2]+x[3]+x[4]+x[5])
G[2]←K2-((2×x[2])*2)×(P÷Pstd)÷(x[3]+x[5]-x[2])×(3+x[1]+x[2]+x[3]+x[4]+x[5])
G[3]←K3-(((2×x[3])+x[4]-x[1])*2)×(x[3]+x[5]-x[2])×(P÷Pstd)÷ ((2-(2×x[3])+2×x[4])*2)×(3+x[1]+x[2]+x[3]+x[4]+x[5])
G[4]←K4-(x[4]+(2×x[3])-x[1])×((2×x[4])*2)×(P÷Pstd)÷((2-(2×x[3])+2×x[4])*2)×(3+x[1]+x[2]+x[3]+x[4]+x[5])
G[5]←K5-((2×x[5])*2)×(x[3]+x[5]-x[2])×(P÷Pstd)÷((1-2×x[5])*2)×(3+x[1]+x[2]+x[3]+x[4]+x[5])
Output←G
REMINGTON30
 
Posts: 13
Joined: Fri Aug 11, 2017 2:17 pm

Re: Element-by-Element Assignment to Vectors

Postby petermsiegel on Wed Dec 27, 2017 4:10 am

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[5] 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[1] 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[5] 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[1]-(sq 2×x[1])×P∆÷x∆×(2×x[3])+x[4]-x[1]
G2←K[2]-(sq 2×x[2])×P∆÷x∆×x[3]+x[5]-x[2]
G3←K[3]-(sq(2×x[3])+x[4]-x[1])×(x[3]+x[5]-x[2])×P∆÷x∆×sq 2-(2×x[3])+2×x[4]
G4←K[4]-(x[4]+(2×x[3])-x[1])×(sq 2×x[4])×P∆÷x∆×sq 2-(2×x[3])+2×x[4]
G5←K[5]-(sq 2×x[5])×(x[3]+x[5]-x[2])×P∆÷x∆×sq 1-2×x[5]

I tested to make sure it gave the same results as yours using repeated calls to:
      {(F2 ⍵)≡F2a ⍵}?5⍴0
petermsiegel
 
Posts: 51
Joined: Thu Nov 11, 2010 11:04 pm

Re: Element-by-Element Assignment to Vectors

Postby REMINGTON30 on Thu Dec 28, 2017 6:18 pm

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: 13
Joined: Fri Aug 11, 2017 2:17 pm

Re: Element-by-Element Assignment to Vectors

Postby petermsiegel on Thu Dec 28, 2017 11:37 pm

Good luck - and Happy New Year. Glad to see you are on your way to the solution you want.
petermsiegel
 
Posts: 51
Joined: Thu Nov 11, 2010 11:04 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest