Deconvolution/1D: Difference between revisions

m (→‎{{header|Perl}}: v5.36 for subroutine signatures)
 
(5 intermediate revisions by 5 users not shown)
Line 313:
h[] data is : -8 -9 -3 -1 -6 7
deconv(g, f): -8 -9 -3 -1 -6 7</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
 
void print_vector(const std::vector<int32_t>& list) {
std::cout << "[";
for ( uint64_t i = 0; i < list.size() - 1; ++i ) {
std::cout << list[i] << ", ";
}
std::cout << list.back() << "]" << std::endl;
}
 
std::vector<int32_t> deconvolution(const std::vector<int32_t>& a, const std::vector<int32_t>& b) {
std::vector<int32_t> result(a.size() - b.size() + 1, 0);
for ( uint64_t n = 0; n < result.size(); n++ ) {
result[n] = a[n];
uint64_t start = std::max((int) (n - b.size() + 1), 0);
for ( uint64_t i = start; i < n; i++ ) {
result[n] -= result[i] * b[n - i];
}
result[n] /= b[0];
}
return result;
}
 
int main() {
const std::vector<int32_t> h = { -8, -9, -3, -1, -6, 7 };
const std::vector<int32_t> f = { -3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1 };
const std::vector<int32_t> g = { 24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52,
25, -67, -96, 96, 31, 55, 36, 29, -43, -7 };
 
std::cout << "h = "; print_vector(h);
std::cout << "deconvolution(g, f) = "; print_vector(deconvolution(g, f));
std::cout << "f = "; print_vector(f);
std::cout << "deconvolution(g, h) = "; print_vector(deconvolution(g, h));
}
</syntaxhighlight>
{{ out }}
<pre>
h = [-8, -9, -3, -1, -6, 7]
deconvolution(g, f) = [-8, -9, -3, -1, -6, 7]
f = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
deconvolution(g, h) = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
</pre>
 
=={{header|Common Lisp}}==
Line 482 ⟶ 531:
deconv(h, g) = -3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Sub Deconv(g() As Double, f() As Double, h() As Double)
Dim As Integer n, i, lower
Dim As Integer hCount = Ubound(g) - Ubound(f) + 2
Redim h(hCount - 1)
For n = 0 To hCount - 1
h(n) = g(n)
lower = Iif(n >= Ubound(f) + 1, n - Ubound(f), 0)
i = lower
While i < n
h(n) -= h(i) * f(n - i)
i += 1
Wend
h(n) /= f(0)
Next n
End Sub
 
Dim As Integer i
Dim As Double h(5) = {-8, -9, -3, -1, -6, 7}
Dim As Double f(15) = {-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1}
Dim As Double g(20) = {24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52, 25, -67, -96, 96, 31, 55, 36, 29, -43, -7}
Dim As Double result()
 
Print !"h:\n[";
For i = Lbound(h) To Ubound(h)
Print h(i); ",";
Next i
Print Chr(8) & !"]\n";
 
Deconv(g(), f(), result())
Print !"\deconv(g, f):\n[";
For i = Lbound(result) To Ubound(result)-1
Print result(i); ",";
Next i
Print Chr(8) & !"]\n";
 
Print
Print !"f:\n[";
For i = Lbound(f) To Ubound(f)
Print f(i); ",";
Next i
Print Chr(8) & !"]\n";
 
Deconv(g(), h(), result())
Print !"\deconv(g, h):\n[";
For i = Lbound(result) To Ubound(result)-1
Print Using "##_,"; result(i);
Next i
Print Chr(8) & !"]\n";
Sleep</syntaxhighlight>
{{out}}
<pre>h:
[-8,-9,-3,-1,-6, 7]
deconv(g, f):
[-8,-9,-3,-1,-6, 7]
 
f:
[-3,-6,-1, 8,-6, 3,-1,-9,-9, 3,-2, 5, 2,-2,-7,-1]
deconv(g, h):
[-3,-6,-1, 8,-6, 3,-1,-9,-9, 3,-2, 5, 2,-2,-7,-1]</pre>
 
=={{header|Go}}==
Line 770 ⟶ 881:
f = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
deconv(g, h) = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
</pre>
 
=={{header|jq}}==
{{trans|Wren}}
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
<syntaxhighlight lang="jq">
def deconv($g; $f):
{ h: [range(0; ($g|length) - ($f|length) + 1) | 0] }
| reduce range ( 0;.h|length) as $n (.;
.h[$n] = $g[$n]
| (if $n >= ($f|length) then $n - ($f|length) + 1 else 0 end) as $lower
| .i = $lower
| until(.i >= $n;
.h[$n] -= .h[.i] * $f[$n - .i]
| .i += 1 )
| .h[$n] /= $f[0] )
| .h ;
 
### The tasks
 
def h: [-8, -9, -3, -1, -6, 7];
def f: [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1];
def g: [24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52, 25, -67, -96, 96, 31, 55, 36, 29, -43, -7];
 
h,
deconv(g; f),
f,
deconv(g; h)
</syntaxhighlight>
{{output}}
<pre>
[-8,-9,-3,-1,-6,7]
[-8,-9,-3,-1,-6,7]
[-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1]
[-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1]
</pre>
 
Line 1,383 ⟶ 1,534:
array F: -3 -6 -1 8 -6 3 -1 -9 -9 3 -2 5 2 -2 -7 -1
array G: 24 75 71 -34 3 22 -45 23 245 25 52 25 -67 -96 96 31 55 36 29 -43 -7
</pre>
 
=={{header|RPL}}==
{{trans|D}}
When translating to RPL, it is mandatory to take into account that:
* array indexes start at 1
* For loop variables, j shall be preferred to i, which is the name of the internal constant that equals √-1
* FOR..NEXT loops are executed at least once
≪ → g f
≪ g SIZE f SIZE - 1 + 1 →LIST 0 CON
1 g 1 GET f 1 GET / PUT
2 OVER SIZE '''FOR''' n
g n GET
1 n f SIZE - 0 MAX +
n 1 - '''FOR''' j
OVER j GET
f n j - 1 + GET * -
'''NEXT'''
f 1 GET / n SWAP PUT
'''NEXT'''
≫ ≫ '<span style="color:blue">DECONV</span>' STO
 
≪ [-8 -9 -3 -1 -6 7]
[-3 -6 -1 8 -6 3 -1 -9 -9 3 -2 5 2 -2 -7 -1]
[24 75 71 -34 3 22 -45 23 245 25 52 25 -67 -96 96 31 55 36 29 -43 -7]
→ h f g
≪ g f <span style="color:blue">DECONV</span> h ==
g h <span style="color:blue">DECONV</span> f == AND
≫ ≫ ‘<span style="color:blue">TASK</span>’ STO
{{out}}
<pre>
1: 1
</pre>
 
Line 1,639 ⟶ 1,822:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn main() {
h := [f64(-8), -9, -3, -1, -6, 7]
f := [f64(-3), -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
Line 1,678 ⟶ 1,861:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var deconv = Fn.new { |g, f|
var h = List.filled(g.count - f.count + 1, 0)
for (n in 0...h.count) {
2,467

edits