Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic-size compatible #14

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/FFT_implementation/fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ int main()
{
// const Complex a[] = {cd(0, 0), cd(1, 1), cd(3, 3), cd(4, 4), cd(4, 4), cd(3, 3), cd(1, 1), cd(0, 0)};
const Complex a[] = {cd(1,0),cd(-1,0),cd(0,1),cd(0,-1)};
CArray x(a, 4);
const Complex a[] = {cd(1,0),cd(-1,0),cd(0,1),cd(0,-1),cd(1,0),cd(2,1),cd(3,3),cd(2,0)};
CArray x(a, 8);
fft(x);
for (auto it : x)
{
Expand Down
6 changes: 3 additions & 3 deletions src/FFT_implementation/fft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ contract FFT {
return log_val;
}

function fft(int256[4] memory real_part, int256[4] memory complex_part)
function fft(int256[] memory real_part, int256[] memory complex_part)
public
view
returns (int256[4] memory, int256[4] memory)
returns (int256[] memory, int256[] memory)
{
uint256 N = real_part.length;
uint256 k = N;
Expand All @@ -43,7 +43,7 @@ contract FFT {
int256 phiT_real__temp = phiT.real;
phiT.real = (((phiT.real * phiT.real) / 1e18) - ((phiT.img * phiT.img) / 1e18));
phiT.img = (2 * phiT.img * phiT_real__temp) / 1e18;
if (k == 2) require(phiT.real / 1e12 == 0);
// if (k == 2) require(phiT.real / 1e12 == 0);
T.real = 1 * 1e18;
T.img = 0 * 1e18;
for (uint256 l = 0; l < k; l++) {
Expand Down
59 changes: 54 additions & 5 deletions test/FFT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import "../src/complexMath/Trigonometry.sol";

contract FFTTest is Test {
FFT public fft;
int256[] real_part;
int256[] complex_part;

function setUp() public {
fft = new FFT();
}

function test_finalFFT() public {
int256[4] memory real_part = [int256(1) * 1e18, -1 * 1e18, 0 * 1e18, 0 * 1e18];
int256[4] memory complex_part = [int256(0) * 1e18, 0 * 1e18, 1 * 1e18, -1 * 1e18];
int256[4] memory re;
int256[4] memory im;
function test_finalFFT1() public {
real_part.push((int256(1) * 1e18));
real_part.push(-1 * 1e18);
real_part.push(0 * 1e18);
real_part.push(0 * 1e18);
complex_part.push(int256(0) * 1e18);
complex_part.push(0 * 1e18);
complex_part.push(1 * 1e18);
complex_part.push(-1 * 1e18);
int256[] memory re;
int256[] memory im;
(re, im) = fft.fft(real_part, complex_part);
assertApproxEqAbs(re[0], 0 * 1e18, 1e15);
assertApproxEqAbs(im[0], 0 * 1e18, 1e15);
Expand All @@ -26,6 +34,47 @@ contract FFTTest is Test {
assertApproxEqAbs(im[2], 2 * 1e18, 1e15);
assertApproxEqAbs(re[3], 0 * 1e18, 1e15);
assertApproxEqAbs(im[3], -2 * 1e18, 1e15);
// real_part=;
// complex_part=[];
}

function test_finalFFT2() public {
real_part.push(1 * 1e18);
real_part.push(-1 * 1e18);
real_part.push(0 * 1e18);
real_part.push(0 * 1e18);
real_part.push(1 * 1e18);
real_part.push(2 * 1e18);
real_part.push(3 * 1e18);
real_part.push(2 * 1e18);
complex_part.push(0 * 1e18);
complex_part.push(0 * 1e18);
complex_part.push(1 * 1e18);
complex_part.push(-1 * 1e18);
complex_part.push(0 * 1e18);
complex_part.push(1 * 1e18);
complex_part.push(3 * 1e18);
complex_part.push(0 * 1e18);
assertEq(real_part.length, 8);
int256[] memory re;
int256[] memory im;
(re, im) = fft.fft(real_part, complex_part);
assertApproxEqAbs(re[0], 8 * 1e18, 1e15);
assertApproxEqAbs(im[0], 4 * 1e18, 1e15);
assertApproxEqAbs(re[1], -4.121 * 1e18, 1e15);
assertApproxEqAbs(im[1], 6.535 * 1e18, 1e15);
assertApproxEqAbs(re[2], 1 * 1e18, 1e15);
assertApproxEqAbs(im[2], -3 * 1e18, 1e15);
assertApproxEqAbs(re[3], 1.292 * 1e18, 1e15);
assertApproxEqAbs(im[3], 0.535 * 1e18, 1e15);
assertApproxEqAbs(re[4], 2 * 1e18, 1e15);
assertApproxEqAbs(im[4], 4 * 1e18, 1e15);
assertApproxEqAbs(re[5], 0.1213 * 1e18, 1e15);
assertApproxEqAbs(im[5], -0.5355 * 1e18, 1e15);
assertApproxEqAbs(re[6], -3 * 1e18, 1e15);
assertApproxEqAbs(im[6], -5 * 1e18, 1e15);
assertApproxEqAbs(re[7], 2.7071 * 1e18, 1e15);
assertApproxEqAbs(im[7], -6.5355 * 1e18, 1e15);
}
// // function test_trig() public {
// // int sinVal = Trigonometry.sin(15707963267948966150);
Expand Down