How to define a function that can handle a vector/matrix
Subject: How to define a function that can handle a vector/matrix
From: Robert Dodier
Date: Sun, 4 Jun 2006 10:23:47 -0600
On 6/4/06, Fabian Schuh <Fabian.Schuh at gmx.de> wrote:
> f(x) := matrix(
> [x_1 + x_2] ,
> [x_1 - x_2]
> );
Subscripted variables are written with square brackets,
e.g. x[1], x[2], etc.
So in this example,
f (x) := matrix ([x[1] + x[2]], [x[1] - x[2]]);
x : matrix ([1], [2]);
f (x); => matrix ([[3]], [[-1]])
y : [1, 2];
f (y); => matrix ([3], [-1]) <-- Note that this is different from f (x)
When x is a matrix, x[i] (i.e., with one subscript) is a list,
namely the elements in row i. x[i] is a list even if the row
contains just one element. So I'm guessing in this example
you want the argument to be a list (like y) and not a matrix.
Hope this helps,
Robert Dodier