Skip to content

Instantly share code, notes, and snippets.

@YukiSakamoto
Last active February 13, 2021 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YukiSakamoto/39a259bff3f3188d887c74a7abbf4dda to your computer and use it in GitHub Desktop.
Save YukiSakamoto/39a259bff3f3188d887c74a7abbf4dda to your computer and use it in GitHub Desktop.
sincos_add
*.eps
*.pdf
*.png
*.dat
#!/bin/sh
for f in `ls *.eps`
do
pstopdf $f
base=`basename $f .eps`
pdffile=$base.pdf
outfile=$base.png
sips -s format png $pdffile -o $outfile
done
#include <cmath>
#include <cstdio>
#include <cstdlib>
inline double rad(double degree)
{ return degree*M_PI/180; }
inline double sin_addition(double s1, double c1, double s2, double c2)
{ return s1*c2 + c1*s2; }
inline double cos_addition(double s1, double c1, double s2, double c2)
{ return c1*c2 - s1*s2; }
void sincos_by_addition(const int times, const int dr)
{
if (times < 0) { throw; }
double sin_dr = std::sin( rad(static_cast<double>(dr)) );
double cos_dr = std::cos( rad(static_cast<double>(dr)) );
int r = 0;
double sinr = 0.;
double cosr = 1.;
std::printf("#%s \t%20s \t%20s \t%20s \t%20s\n", "r", "sin", "cos", "std::sin", "std::cos");
for(int i = 0; i < times; i++){
std::printf("%d \t%.16f \t%.16f \t%.16f \t%.16f\n", r, sinr, cosr,
std::sin(rad(static_cast<double>(r))),
std::cos(rad(static_cast<double>(r))) );
// next value
double sinr_ = sin_addition(sinr, cosr, sin_dr, cos_dr);
cosr = cos_addition(sinr, cosr, sin_dr, cos_dr);
sinr = sinr_;
r += dr;
}
std::printf("%d \t%.16f \t%.16f \t%.16f \t%.16f\n", r, sinr, cosr,
std::sin(rad(static_cast<double>(r))),
std::cos(rad(static_cast<double>(r))) );
return;
}
int main(int argc, char **argv)
{
// 第一引数は角度の刻み、第二引数は360*n°まで計算
int dr = 3;
int n = 6;
if (1 < argc) {
dr = std::atoi(argv[1]);
if (2 < argc) {
n = std::atoi(argv[2]);
}
}
std::printf("# dr: %d, n: %d\n", dr, n);
int r = 360 * n / dr;
sincos_by_addition(r, dr);
return 0;
}
all:
g++ main.cpp -o main.x
run:
./main.x 1 10 > dr1.dat
./main.x 2 10 > dr2.dat
./main.x 3 10 > dr3.dat
./main.x 10 10 > dr10.dat
gnuplot plot.in
gnuplot plot_diff.in
sh conv.sh
set yrange [-1.2:1.2]
set xtics 360
set mxtics 3
set ytics 0.2
set grid xtics ytics
i = 1;
ofile = sprintf("r%d.eps", i)
ifile = sprintf("dr%d.dat", i)
set terminal postscript enhanced color
set output ofile
plot \
ifile using 1:2 with linespoints pt 7 ps 0.5 title "sin",\
ifile using 1:3 with linespoints pt 7 ps 0.5 title "cos",\
ifile using 1:4 with linespoints pt 7 ps 0.3 title "std::sin",\
ifile using 1:5 with linespoints pt 7 ps 0.3 title "std::cos",\
i = 3;
ofile = sprintf("r%d.eps", i)
ifile = sprintf("dr%d.dat", i)
set terminal postscript enhanced color
set output ofile
plot \
ifile using 1:2 with linespoints pt 7 ps 0.5 title "sin",\
ifile using 1:3 with linespoints pt 7 ps 0.5 title "cos",\
ifile using 1:4 with linespoints pt 7 ps 0.3 title "std::sin",\
ifile using 1:5 with linespoints pt 7 ps 0.3 title "std::cos",\
i = 10;
ofile = sprintf("r%d.eps", i)
ifile = sprintf("dr%d.dat", i)
set terminal postscript enhanced color
set output ofile
plot \
ifile using 1:2 with linespoints pt 7 ps 0.5 title "sin",\
ifile using 1:3 with linespoints pt 7 ps 0.5 title "cos",\
ifile using 1:4 with linespoints pt 7 ps 0.3 title "std::sin",\
ifile using 1:5 with linespoints pt 7 ps 0.3 title "std::cos",\
set terminal postscript enhanced color
set output "diff.eps"
set xtics 360
set mxtics 3
set grid xtics ytics
plot \
"dr1.dat" using 1:(abs($4-$2)) with linespoints ps 0.5 title "dr=1 abs(sin(x)-std::sin(x))",\
"dr3.dat" using 1:(abs($4-$2)) with linespoints ps 0.5 title "dr=3 abs(sin(x)-std::sin(x))",\
"dr10.dat" using 1:(abs($4-$2)) with linespoints ps 0.5 title "dr=10 abs(sin(x)-std::sin(x))",\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment