javaojalgo

How to use abs, sqrt etc on OperateOnAll with OjAlgo?


I'm trying to take square root on all elements in a matrix A by doing the following command.

A.operateOnAll()

But the argument must be UnaryFunction<N> and I don't know how to create that. I reading the API documention, but still don't know how to create a sqrt procedure for matrix A

https://javadoc.scijava.org/ojAlgo/index.html?org/ojalgo/function/FunctionSet.html


Solution

  • UnaryFunction<Double> modifier = PrimitiveMath.ROOT.parameter(2);
    
    // To modify A in place
    A.modifyAll(modifier);
    
    // The results in another matrix
    A.operateOnAll(modifier).supplyTo(B);
    
    // To have a new results matrix created for you
    MatrixStore<Double> C = A.operateOnAll(modifier).get();
    
    // If A if of an immutable type like Primitive64Matrix
    DenseReceiver mutable = A.copy();
    mutable.modifyAll(modifier);
    Primitive64Matrix B = mutable.get();