3

I'm trying to write a few different functions, such as standard deviation and linear regression in APL. I need to pass in a list of (x, y) points, but I can't figure out how to do that because I know the syntax of an APL function only allows for 0, 1, or 2 arguments to be passed in. Is there any kind of array I can use in APL to pass in the list as an array?

Lisa
  • 33
  • 1
  • 4

2 Answers2

4

There are several valid ways of doing this. E.g. using two lists, one for x values and one for y values:

      ∇ c←x LinReg y
        c←⌽y⌹1,[1.5]x
      ∇
      1 3 2 LinReg 2 8 5
3 ¯1

You could also pass in a single matrix where each row represents an (x,y) pair:

      ∇ c←LinReg xy
        c←⌽xy[;2]⌹1,[1.5]xy[;1]
      ∇
      LinReg 3 2⍴1 2,3 8,2 5
3 ¯1

If your APL supports it, you can also use a list of (x,y) pairs. E.g. in Dyalog APL:

      ∇ c←LinReg xys
        c←⌽(2⊃¨xys)⌹1,[1.5](1⊃¨xys)
      ∇
      LinReg (1 2)(3 8)(2 5)
3 ¯1

However, note that this is a particularly inefficient way of representing points in APL.

Adám
  • 6,573
  • 20
  • 37
1

An elegant way to solve this in APL2 is to use vector assignment. For example:

    A ← 42 ◊ B ← 'Hello' ◊ C ← 1 2 3   ⍝ multiple arguments A, B, and C
  
    ∇FOO ARG
[1]  (A B C) ← ARG    ⍝ expand arguments into individuals
[2]  'A:' A
[3]  'B:' B
[4]  'C:' C
[5] ∇
      
      FOO (A B C)
 A: 42 
 B: Hello 
 C:  1 2 3 
Jürgen
  • 11
  • 1