public void fit ( double samplePoints [ ] , double [ ] observations ) { // Create a copy of the observations and put it into a matrix y . reshape ( observations . length , 1 , false ) ; System . arraycopy ( observations , 0 , y . data , 0 , observations . length ) ; // reshape the matrix to avoid unnecessarily declaring new memory // save values is set to false since its old values don't matter A . reshape ( y . numRows , coef . numRows , false ) ; // set up the A matrix for ( int i = 0 ; i < observations . length ; i ++ ) { double obs = 1 ; for ( int j = 0 ; j < coef . numRows ; j ++ ) { A . set ( i , j , obs ) ; obs *= samplePoints [ i ] ; } } // process the A matrix and see if it failed if ( ! solver . setA ( A ) ) throw new RuntimeException ( "Solver failed" ) ; // solver the the coefficients solver . solve ( y , coef ) ; }
Computes the best fit set of polynomial coefficients to the provided observations .