Using `transform_reduce` correctly translating Javascript code to C++

Joined
Jun 11, 2024
Messages
1
Reaction score
0
I have the following Javascript code:

Code:
let XsHat = Xs.map(function(X){
        return zip(IT.funcs, IT.terms).map(function([f, term]){
            return f(zip(X, term).map(function([x, t]){
                return Math.pow(x, t);
            }).reduce(prod, 1));
        });
    });

So basically if you get the following data:

Code:
    X:
    0.95 3.75
     0.8 7.75
    0.55 5.25
    0.25  4.5
...

and the following Polynomial terms <1.10688, 0.977614> and a Function let's say log what I want to compute is log(pow(0.95, 1.10688) * pow(3.75, 0.977614)).

I wrote this C++ code to do it:

Code:
Eigen::MatrixXd XsHat(X.rows(), Functions.size());
 
std::transform(
        std::execution::par,
        X.rowwise().begin(), X.rowwise().end(),
        XsHat.rowwise().begin(),
        [&](const auto &X_row) {
            Eigen::VectorXd XHat_row(Functions.size());
            std::transform(
                    std::execution::par,
                    Polynomials.begin(), Polynomials.end(),
                    Functions.begin(),
                    XHat_row.data(),
                    [&](const Vector<Scalar> &p, auto f) {
                        Scalar transformedX = std::transform_reduce(
                                std::execution::par,
                                X_row.data(), X_row.data() + X_row.size(),
                                p.begin(),
                                1.0,
                                std::multiplies<>(),
                                [](double x, double p) { std::cout << "pow(" << x << ", " << p << ") = " << std::pow(x, p) << "\n"; return std::pow(x, p); }
                        );
                        std::cout << "Transformed X: " << transformedX << "\n";
                        std::cout << "Function: " << FunctionTypes::toString(f.Type) << "\n";
                        std::cout << "Function result: " << f(transformedX) << "\n\n";
                        return f(transformedX);
                    }
            );
            return XHat_row;
        }
);

But what it seems to be printing:

Code:
pow(0.95, 1.10688) = 0.944806
pow(0.8, 0.977614) = 0.804006
Transformed X: 0.75963
Function: ln
Function result: -0.274924
pow(0.8, 1.10688) = 0.781147
pow(0.55, 0.977614) = 0.55741
Transformed X: 0.435419
Function: ln
Function result: -0.831446
pow(0.55, 1.10688) = 0.515957
pow(0.25, 0.977614) = 0.25788
Transformed X: 0.133055
Function: ln
Function result: -2.01699
So apparently instead of taking the elements in each row, it uses the column values!
What is the mistake exactly in the code above?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,940
Messages
2,570,108
Members
46,574
Latest member
LayneChew

Latest Threads

Top