aboutsummaryrefslogtreecommitdiffstats
path: root/buch/chapters/110-elliptisch/experiments/KN.cpp
blob: 1dcca9ea18c26edddcb9f0dc2fc57cfedb72baed (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * KN.cpp
 *
 * (c) 2022 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule
 */
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
#include <getopt.h>
#include <vector>
#include <gsl/gsl_sf_elljac.h>
#include <gsl/gsl_sf_ellint.h>

namespace KN {

bool	debug = false;

static struct option	longopts[] {
{ "debug",	no_argument,		NULL,	'd' },
{ "N",		required_argument,	NULL,	'N' },
{ "outfile",	required_argument,	NULL,	'o' },
{ "min",	required_argument,	NULL,	'm' },
{ NULL,		0,			NULL,	 0  }
};

double	KprimeK(double k) {
	double	kprime = sqrt(1-k*k);
	if (debug)
		printf("%s:%d: k = %f, k' = %f\n", __FILE__, __LINE__, k, kprime);
	double	v
		=
		gsl_sf_ellint_Kcomp(k, GSL_PREC_DOUBLE)
		/
		gsl_sf_ellint_Kcomp(kprime, GSL_PREC_DOUBLE)
		;
	if (debug)
		printf("%s:%d: KprimeK(k = %f) = %f\n", __FILE__, __LINE__, k, v);
	return v;
}

static const int	L = 100000000;
static const double	h = 1. / L;

double	Kd(double k) {
	double	m = 0;
	if (k < h) {
		m = 2 * (KprimeK(k) - KprimeK(k / 2)) / k;
	} else if (k > 1-h) {
		m = 2 * (KprimeK((1 + k) / 2) - KprimeK(k)) / (1 - k);
		
	} else {
		m = L * (KprimeK(k + h) - KprimeK(k));
	}
	if (debug)
		printf("%s:%d: Kd(%f) = %f\n", __FILE__, __LINE__, k, m);
	return m;
}

double	k1(double y) {
	if (debug)
		printf("%s:%d: Newton for y = %f\n", __FILE__, __LINE__, y);
	double	kn = 0.5;
	double	delta = 1;
	int	n = 0;
	while ((fabs(delta) > 0.000001) && (n < 10)) {
		double	yn = KprimeK(kn);
		if (debug)
			printf("%s:%d: k%d = %f, y%d = %f\n", __FILE__, __LINE__, n, kn, n, yn);
		delta = (yn - y) / Kd(kn);
		if (debug)
			printf("%s:%d: delta = %f\n", __FILE__, __LINE__, delta);
		double	kneu = kn - delta;
		if (kneu <= 0) {
			kneu = kn / 4;
		}
		if (kneu >= 1) {
			kneu = (3 + kn) / 4;
		}
		kn = kneu;
		if (debug)
			printf("%s:%d: kneu = %f, kn = %f\n", __FILE__, __LINE__, kneu, kn);
		n++;
	}
	if (debug)
		printf("%s:%d: Newton result: k = %f\n", __FILE__, __LINE__, kn);
	return kn;
}

double	k1(int N, double k) {
	return k1(KprimeK(k) / N);
}

/**
 * \brief Main function for the slcl program
 */
int	main(int argc, char *argv[]) {
	int	longindex;
	int	c;
	int	N = 5;
	double	kmin = 0.01;
	std::string	outfilename;
	while (EOF != (c = getopt_long(argc, argv, "d:N:o:m:",
		longopts, &longindex)))
		switch (c) {
		case 'd':
			debug = true;
			break;
		case 'N':
			N = std::stoi(optarg);
			break;
		case 'o':
			outfilename = std::string(optarg);
			break;
		case 'm':
			kmin = std::stod(optarg);
			break;
		}

	double	d = 0.01;
	if (outfilename.size() > 0) {
		FILE	*fn = fopen(outfilename.c_str(), "w");
		fprintf(fn, "\\def\\KKpath{ ");
		double	k = d;
		fprintf(fn, " (0,0)");
		double	k0 = k/16;
		while (k0 < k) {
			fprintf(fn, "\n\t-- ({%.4f*\\dx},{%.4f*\\dy})", k0, KprimeK(k0));
			k0 *= 2;
		}
		while (k < 1-0.5*d) {
			fprintf(fn, "\n\t-- ({%.4f*\\dx},{%.4f*\\dy})", k, KprimeK(k));
			k += d;
		}
		fprintf(fn, "}\n");

		k0 = 0.97;
		fprintf(fn, "\\def\\knull{%.4f}\n", k0);
		double	KK = KprimeK(k0);
		fprintf(fn, "\\def\\KKnull{%.4f}\n", KK);
		fprintf(fn, "\\def\\kone{%.4f}\n", k1(N, k0));

		fclose(fn);
		return EXIT_SUCCESS;
	}

	for (double k = kmin; k < (1 - d/2); k += d) {
		if (debug)
			printf("%s:%d: k = %f\n", __FILE__, __LINE__, k);
		double	y = KprimeK(k);
		double	k0 = k1(y);
		double	kone = k1(N, k0);
		printf("g(%4.2f) = %10.6f,", k, y);
		printf("    g'(%.2f) = %10.6f,", k, Kd(k));
		printf("    g^{-1} = %10.6f,", k0);
		printf("    k1 = %10.6f,", kone);
		printf("    g(k1) = %10.6f\n", KprimeK(kone));
	}

	return EXIT_SUCCESS;
}

} // namespace KN

int	main(int argc, char *argv[]) {
	try {
		return KN::main(argc, argv);
	} catch (const std::exception& e) {
		std::cerr << "terminated by exception: " << e.what();
		std::cerr << std::endl;
	} catch (...) {
		std::cerr << "terminated by unknown exception" << std::endl;
	}
	return EXIT_FAILURE;
}