## Vector Arguments in Functions

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

### Vector Arguments in Functions

Consider the simple user-defined function below. The function Poly(x y) works correctly with both x and y scalars. It also works correctly with: (1) x scalar and y vector; and (2) x vector and y scalar. What modification to the function is required get the correct value of the output if both x and y are vectors? If both of the input vectors are rank 1, the output should be a matrix of rank 2.

z←Poly(x y)
z←10+(x*2)+y*3

If this matter is covered in the Dyalog documentation, please provide the reference.
REMINGTON30

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

### Re: Vector Arguments in Functions

Outer Product

z←Poly(x y)
z←10+(x*2)∘.+y*3

And welcome to the forums.

Phil Last

Posts: 448
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Vector Arguments in Functions

Thanks Phil. I have some more complicated functions that work fine with scalar inputs but fail with vector inputs. I may post some of them on the Forum later.
REMINGTON30

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

### Re: Vector Arguments in Functions

I may be about to confuse you by my use of the words "function", "argument" and "operator". APL is an infix notation. Its functions, be they primitive, defined or derived, take one (right) or two (left and right) array arguments and return an array. Your definition of Poly uses a fairly new extension to Dyalog APL that permits non-APL-like functional notation whose (x y) is not two arguments but one. Poly is monadic.

`      z←x Poly y`
your function would be identical internally but called with the arguments either side, that is, dyadic . It would then be applicable to various operators: reduction (/) scan (\) outer (∘.) and inner product (.) &c. which take dyadic functions as operands.

Functions
`      + - × ÷ | ⌊ ⌈ * ⍟ ○ ! ~ ? ∊ ^ ∨ ⍲ ⍱ < ≤ = ≥ > ≠`
are all scalar functions which means they return an array that conforms to their single (right) argument or both conforming (left and right) arguments - where conforming means agreeing in rank and shape or having one argument a scalar.

If you write a function that uses only primitive scalar functions then your function is guaranteed to be scalar itself. Had you written Poly as
`      ∇ z←x Poly y[1]    z←10+(x*2)+y*3      ∇`
you wouldn't need to amend it to take advantage of the above.
`      1 Poly 2 ⍝ scalar/scalar19       1 Poly 2 3 4 ⍝ scalar/vector19 38 75       1 2 3 Poly 2 ⍝ vector/scalar19 22 27       1 2 3 Poly 2 3 4 ⍝ conforming vectors19 41 83       1 2 3 Poly 2 3 4 5 ⍝ non-conforming vectorsLENGTH ERRORPoly[1] z←10+(x*2)+y*3          ∧      →       1 2 3 ∘.Poly 2 3 4 5 ⍝ non-conforming - outer product19 38 75 13622 41 78 13927 46 83 144`

Phil Last

Posts: 448
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Vector Arguments in Functions

Thank you Phil. I am a beginner at APL and have have only about two months of part-time practice and no professional instruction. Learning from 1970's and 1980's books on APL and the excellent Dyalog documents has been a good experience. At this point, a few of my old FORTRAN and Mathcad codes have been successfully coded in APL.

I have learned a great deal from your comments. Many of the functions I have written use the Namelist type of right argument Function(x y z w). I will try converting some of them to a dyadic form X Function Y with both X and Y vectors. One current function I have solves the supersonic conical flow problem including the conical shock wave angle, corresponding cone angle, and all temperature, pressure, and velocity conditions between the shock wave and the cone surface. The function works great as long as all of the input arguments (currently a Namelist), including the upstream Mach number, are scalar. Based on your comments, I will try to modify the function to accept a vector input for the Mach number and, perhaps, other input arguments as well.

Again, thank you.
Attachments
Conical Shock Wave
Shock on cone image022_small.jpg (8.29 KiB) Viewed 792 times
REMINGTON30

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