File: Fourier.pm

package info (click to toggle)
dmrgpp 6.06-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 113,900 kB
  • sloc: cpp: 80,986; perl: 14,772; ansic: 2,923; makefile: 83; sh: 17
file content (103 lines) | stat: -rwxr-xr-x 1,819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl

use strict;
use warnings;
use utf8;
use Math::Trig;

package Fourier;

my $pi = Math::Trig::pi;

sub printArray
{
	my ($array) = @_;
	my $n = scalar(@$array);
	for (my $m = 0; $m < $n; ++$m) {
		my $q = 2*$pi*$m/$n;
		print "$q ".$array->[$m]->[0]." ".$array->[$m]->[1]."\n";
	}
}

sub fourierTransform
{
	my ($array) = @_;
	my $n = scalar(@$array);
	my @sq;
	my $c = int($n/2);
	for (my $m = 0; $m < $n; ++$m) {

		my ($sumReal, $sumImag) = (0, 0);

		for (my $i = 0; $i < $n; ++$i) {
			my $ptr1 = $array->[$i];
			for (my $c = 0; $c < $n; ++$c) {
				my $ptr2 = $array->[$c];
				die "$0: undefined for $i \n" if (!defined($ptr1));
				die "$0: undefined for $c \n" if (!defined($ptr2));

				my $value = ($i < $c) ? $array->[$i]->[$c] : $array->[$c]->[$i];

				$sumReal += cos(2*$pi*($i-$c)*$m/$n)*$value;
				$sumImag += sin(2*$pi*($i-$c)*$m/$n)*$value;
			}
		}

		$sq[$m] = [$sumReal/$n, $sumImag/$n];
	}

	return @sq;
}

sub loadMatrix
{
	my ($file, $label) = @_;
	open(FILE, "<", "$file") or die "$0: Cannot open $file : $!\n";
	my @m;
	my $found = 0;
	while (<FILE>) {
		next if (/cmdline:/i);
		if (/^\Q$label/) {
			$found = 1;
			last;
		}
	}

	if (!$found) {
		close(FILE);
		die "$0: Cannot find $label in $file\n"
	}

	$_ = <FILE>;
	chomp;
	my @temp = split;
	if (scalar(@temp) != 2) {
		close(FILE);
		die "$0: Expected 2 sizes found @temp instead\n";
	}

	my $rows = $temp[0];
	my $cols = $temp[1];

	while (<FILE>) {
		chomp;
		my @temp = split;
		last if (scalar(@temp) != $cols);
		#print @temp;
		#print "\n";
		push @m, \@temp;
	}

	close(FILE);
	#print "----------\n";
	my $r = scalar(@m);
	if ($r != $rows) {
		die "$0: Expected $rows rows but found $r instead\n";
	}

	print STDERR "$0: Read matrix $label from $file with $rows rows and $cols columns.\n";
	return @m;
}

1;