I have a task to which I found a solution but I am not sure there is any there optimal (faster) way of computing.
Problem:
Obtain:
https://latex.codecogs.com/gif.latex?D_{i,j}&space;=&space;\left\{&space;\begin{matrix}&space;A_{i,j}&space;&&space;\textup{&space;for&space;}&space;C_{i,j}
where A, B, C, and D are are matrices of same dimensions and t is a scalar.
My solution:
let A = math.evaluate("[1, 2; 3, 4]");
let B = math.evaluate("[5, 6; 7, 8]");
let C = math.evaluate("[10, 20; 5, 25]");
let t = 15;
let x = math.smaller(C, t);
let y = math.number(x);
let z = math.subtract(1, y);
let p = math.dotMultiply(A, y);
let q = math.dotMultiply(B, z);
let D = math.add(p, q);
Question:
Is there any other way of performing this?
For example I notice that I don't need to calculate y because math.js interprets false as 0 and true as 1. So, using y = x and z = math.not(x) may improve performance. Please enlighten me if I can improve further.
Thank you.
If you want to do element wise operations you can maybe use map?
Thank you :)
I think map will improve the performance. Is it possible to in the future include the performance and memory requirement information in the documentation. It would be very useful to take decision to select which operation is most optimal for performance or memory. (Since modern computers have enough memory, performance info would be more useful.)
Thanks for your suggestions. I'm not sure how we can document performance in a useful way: it depends strongly on how large matrices you're using, which operations you're performing, what datatypes you're using etc etc. In general, if you're writing performance critical code, it's best to do some real-world benchmarking on the case at hand I think.
Ok to close this issue?
Thank you for your reply.