For example, I'd like to take the first four upper left values of a matrix, i.e.
f[x_] := f[x] = x[[;; 2, ;; 2]];
This works nicely, e.g. f[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]= {{1, 2}, {4, 5}}
Now, I was trying to ged rid of the delayed evaluation and just use f[x_]=... which doesn't work: Part::take: Cannot take positions 1 through 2 in x.
But if I define the function like this, it works:
f = #[[;; 2, ;; 2]]&;
Why is this? And how would I do it in the f[x_]=pattern?
Of course, I could just use delayed evaluation but in another case I have a more complicated funtion
g[x_,y_]=x[[;; 2, ;; 2]]+expensiveFunction[y]
where I would like the g[x]part to be evaluated delayed and the g[y] part evaluated at the time of definition.
Ultimately, I would even like Mathematica to recognize that by x[[;; 2, ;; 2]] I have a 2x2 matrix and further functions of that 2x2 matrix are already evaluated without knowing the values there (e.g. determinant or trace):
h[x_,y_]=expensiveFunction1[x[[;; 2, ;; 2]]]+expensiveFunction2[y]
f[x_] := x[[;; 2, ;; 2]];– Mr.Wizard Feb 17 '17 at 14:35[Note,
– riddleculous Feb 17 '17 at 14:45SetAttributes[f, HoldFirst]; f[x_] = x[[;; 2, ;; 2]];(as you just suggested before editing) gives the same error.]f[x_] := f[x] = x[[;; 2, ;; 2]];does not do what you want. – Mr.Wizard Feb 17 '17 at 14:48