I've written a small function that basically applies a calculation to all elements of an array using
Zero's eval function.
Here it is:
PHP Code:
function evalArray(arr, ev) {
if (arr[0].size() == 0)
arr = { arr };
// evaluate all array elements
temp.nArr = new[arr[0].size()];
for (temp.i = 0; i < arr[0].size(); i++) {
temp.eva = ev;
for (temp.k = 0; k < eva.size(); k++)
for (temp.l = 0; l < arr.size(); l++)
if (eva[k] == "E" @ l)
eva[k] = arr[l][i];
nArr[i] = evalPost(eva);
}
return nArr;
}
The first argument is either a single array of values to use in the calculations or an array of multiple arrays holding values (if you want to use more than one). The function will return an array the size of the (first) value array, holding all the solutions.
The second argument is your formula in post-fix notation (more on that in Zero's thread). In this array you can use E0, E1, E2 and so on to refer to the current array element of the specific array (starting at 0, obviously) depending on how many arrays you've given to the function.
Example:
PHP Code:
temp.a1 = { 1, 2, 3 };
temp.a2 = { 4, 5, 6 };
echo(evalArray({ a1, a2 }, { "E0", "E1", "+", 2, "*" })); // returns { 10, 14, 18 }
Maybe this is useful for some of you guys. I've written it for a project of mine.