Vector Arguments in Functions

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

Vector Arguments in Functions

Postby REMINGTON30 on Fri Sep 08, 2017 7:40 pm

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

Re: Vector Arguments in Functions

Postby Phil Last on Sat Sep 09, 2017 1:31 pm

Outer Product

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


And welcome to the forums.
User avatar
Phil Last
 
Posts: 624
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Vector Arguments in Functions

Postby REMINGTON30 on Mon Sep 11, 2017 10:56 pm

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

Re: Vector Arguments in Functions

Postby Phil Last on Tue Sep 12, 2017 7:26 am

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.

If its header were
      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/scalar
19
1 Poly 2 3 4 ⍝ scalar/vector
19 38 75
1 2 3 Poly 2 ⍝ vector/scalar
19 22 27
1 2 3 Poly 2 3 4 ⍝ conforming vectors
19 41 83
1 2 3 Poly 2 3 4 5 ⍝ non-conforming vectors
LENGTH ERROR
Poly[1] z←10+(x*2)+y*3


1 2 3 ∘.Poly 2 3 4 5 ⍝ non-conforming - outer product
19 38 75 136
22 41 78 139
27 46 83 144
User avatar
Phil Last
 
Posts: 624
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Vector Arguments in Functions

Postby REMINGTON30 on Tue Sep 12, 2017 10:42 pm

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
Shock on cone image022_small.jpg
Conical Shock Wave
Shock on cone image022_small.jpg (8.29 KiB) Viewed 14615 times
REMINGTON30
 
Posts: 22
Joined: Fri Aug 11, 2017 2:17 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest