69 lines
2.8 KiB
Plaintext
69 lines
2.8 KiB
Plaintext
/*
|
|
Copyright (c) 2015 Thomas Baruchel
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
the Software without restriction, including without limitation the rights to
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
|
so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
|
|
|
|
Adapted from: http://axiom-wiki.newsynthesis.org/RationalInterpolation
|
|
*/
|
|
|
|
/* version 1 */
|
|
/*
|
|
rinterp(xlist, ylist, m, k) := block([collist, i, j, x, l, ol],
|
|
if length(xlist) # length(ylist)
|
|
then error("Different number of points and values."),
|
|
if length(xlist) # m+k+1
|
|
then error("Wrong number of points."),
|
|
collist: genmatrix(lambda([i,j],1), m+k+1, m+k+2),
|
|
for j:1 thru max(m,k) do
|
|
for i: 1 thru m+k+1 do
|
|
collist[i,j+1]: collist[i,j] * xlist[i],
|
|
for j:1 thru k+1 do
|
|
for i:1 thru m+k+1 do
|
|
collist[i,m+k+3-j]: -collist[i,k+2-j]*ylist[i],
|
|
x: makelist( gensym(), j, m+k+2 ),
|
|
ol: linsolve_params, linsolve_params: true,
|
|
l: linsolve(makelist(sum( x[j]*collist[i,j], j, 1, m+k+2 ), i, m+k+1), x),
|
|
for j:1 thru length(%rnum_list) do l:subst(1, %rnum_list[j], l),
|
|
linsolve_params: ol,
|
|
sum( rhs(l[j])*('x)^(j-1), j, 1, m+1 ) /
|
|
sum( rhs(l[m+1+j])*('x)^(j-1), j, 1, k+1 ));
|
|
*/
|
|
|
|
/* version 2 */
|
|
rinterp(xlist, ylist, m, k) := block([collist, i, j, x, l, ol],
|
|
if length(xlist) # length(ylist)
|
|
then error("Different number of points and values."),
|
|
if length(xlist) # m+k+1
|
|
then error("Wrong number of points."),
|
|
collist: genmatrix(lambda([i,j],1), m+k+2, m+k+1),
|
|
for j:1 thru max(m,k) do
|
|
for i: 1 thru m+k+1 do
|
|
collist[j+1,i]: collist[j,i] * xlist[i],
|
|
for j:1 thru k+1 do
|
|
for i:1 thru m+k+1 do
|
|
collist[m+k+3-j,i]: -collist[k+2-j,i]*ylist[i],
|
|
x: genmatrix(lambda([i,j],gensym()), 1, m+k+2),
|
|
ol: linsolve_params, linsolve_params: true,
|
|
l:linsolve(list_matrix_entries(x.collist), list_matrix_entries(x)),
|
|
for j:1 thru length(%rnum_list) do l:subst(1, %rnum_list[j], l),
|
|
linsolve_params: ol,
|
|
sum( rhs(l[j])*('x)^(j-1), j, 1, m+1 ) /
|
|
sum( rhs(l[m+1+j])*('x)^(j-1), j, 1, k+1 ));
|