Bilinear interpolation: Difference between revisions

J solution
No edit summary
(J solution)
Line 101:
im.rescaleGray(1.3, 1.8).savePGM("lena_larger.pgm");
}</lang>
 
=={{header|J}}==
<lang J>
Note 'FEA'
The interpolant is the dot product of the four function values with the values at the nodes.
Sum of four linear functions of two variables (xi, eta) is one at each of 4 nodes.
Let the base element have nodal coordinates (corners) (x,y) with at +/-1.
 
 
2 3 (1,1)
+---------------+
| |
| |
| (0,0) |
| * |
| |
| |
| |
+---------------+
0 1
 
determine f0(xi,eta), ..., f3(xi,eta).
f0(-1,-1) = 1, f0(all other corners) is 0.
f1( 1,-1) = 1, f1(all other corners) is 0.
...
)
 
f0 =: 1r4 * [: */ 1 1&- NB. f0(xi,eta) = (1-xi)(1-eta)/4
f1 =: _1r4 * [: */ _1 1&- NB. f1(xi,eta) = (1+xi)(1-eta)/4
f2 =: _1r4 * [: */ 1 _1&- NB. ...
f3 =: 1r4 * [: */ _1 _1&-
 
functions =: f0`f1`f2`f3`:0
 
CORNERS =: 21 A. -.+:#:i.4
NB. prove the function values at the corners are correct.
assert (=i.4) -: functions"1 CORNERS NB. 4 by 4 identity matrix
 
interpolate =: (+/ .*) functions
</lang>
<pre>
Note. 'demonstrate the interpolant with a saddle'
lower left has value 1,
lower right: 2
upper left: 2.2
upper right: 0.7
)
 
GRID =: |.,~"0/~(%~i:)100
SADDLE =: 1 2 2.2 0.7 interpolate"_ 1 GRID
viewmat SADDLE
</pre>
[[Image:J_bilinear_interpolant.jpg]]
 
=={{header|Racket}}==
Anonymous user