Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Surface fitting with PDL

by pryrt (Abbot)
on Aug 09, 2020 at 21:17 UTC ( [id://11120528]=note: print w/replies, xml ) Need Help??

in reply to Surface fitting with PDL


Because you invoked "least squares fitting", I assume what you want is a planar least-squares fitting in 3d, analogous to the linear least-squares in 2d, and that z is supposed be be linearly dependent on the others: ax+by+c = z

You can see the algebra in this math.stackexchange answer, but basically you are solving A*X=B, where A is a matrix of the x and y data, X are the column-vector of the coefficients a,b,c from the above equation, and B is the column-vector of the z values for each x,y input.

I haven't looked up the exact PDL syntax, but: An exact solution for 3 sets of (x,y,z) data would have PDL akin to $coeff_X = $matrix_A->inverse * $column_B;. But since you presumably have many points, not just three, since you invoked best-fit, you have to use the "left pseudo inverse", which would have a PDL implementation akin to $coeff_X = ($matrix_A->transpose * $matrix_A)->inverse * ($matrix_A->transpose) * $column_B;

update: I was close.

#!/usr/bin/perl use strict; use warnings; use PDL; use PDL::Matrix; # example: 2x + 3y + 4 = z, plug in known exact values; make sure my c +oefficients end up 2,3,4 my $matrix_A = mpdl [ [1,1,1], [3,7,1], [5,1,1], [1,5,1], ]; print "A => ", $matrix_A; print "B => ", my $column_B = vpdl [9,31,17,21]; print "X => ", my $coeff_X = (($matrix_A->transpose x $matrix_A)->inv +x $matrix_A->transpose) x $column_B;


A => [ [1 1 1] [3 7 1] [5 1 1] [1 5 1] ] B => [ [ 9] [31] [17] [21] ] X => [ [ 2] [ 3] [ 4] ]

Replies are listed 'Best First'.
Re^2: Surface fitting with PDL
by Xilman (Hermit) on Aug 10, 2020 at 07:29 UTC

    Excellent! Many thanks.

    I actually wish to fit a higher order polynomial, perhaps up to a cubic with cross terms, but the generalization to be made to your code should be straightforward.

Re^2: Surface fitting with PDL
by etj (Deacon) on Apr 21, 2022 at 07:23 UTC
    Note for posterity: the go-to module for this stuff is PDL::LinearAlgebra. The obvious solver for Ax=B would be "msolve", and the notes for that give which LAPACK function is used "under the hood". For solving many times, you'd want to do a decomposition (quite possibly an LU) first, then reuse it. Take a look!

    LAPACK is a huge and powerful library for linear-algebra stuff, and getting to know it will be useful, regardless of which language environment you use - I don't think there is a single language environment that doesn't have a binding for it.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120528]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-24 03:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found