fractal/criticals.c

70 lines
1.4 KiB
C
Raw Permalink Normal View History

2020-03-07 23:32:57 +01:00
/*
* fractal
* Written in 2020 by Lucas
* CC0 1.0 Universal/Public domain - No rights reserved
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty. You should have received a copy of the CC0 Public Domain
* Dedication along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "criticals.h"
#include "util.h"
2020-03-09 01:28:41 +01:00
#define MAX_CRITICALS 100
2020-03-07 23:32:57 +01:00
static size_t idx = 0;
2020-03-09 01:28:41 +01:00
static long double complex criticals[MAX_CRITICALS];
2020-03-07 23:32:57 +01:00
int
2020-03-09 01:32:30 +01:00
criticals_test(long double complex (*df)(long double complex),
2020-03-07 23:32:57 +01:00
long double complex (*ddf)(long double complex), long double complex z,
long double complex *crit, long niters, long double bailout,
long double epsilon)
2020-03-07 23:32:57 +01:00
{
long i;
for (i = 0; i < niters && cabsl(z) < bailout; i++)
2020-03-07 23:32:57 +01:00
z = z - df(z) / ddf(z);
if (cabsl(df(z)) < epsilon) {
*crit = z;
return 1;
}
return 0;
}
void
2020-03-09 01:32:30 +01:00
criticals_add(long double complex z)
2020-03-07 23:32:57 +01:00
{
size_t i;
2020-03-09 01:28:41 +01:00
if (idx >= MAX_CRITICALS)
2020-03-07 23:32:57 +01:00
return;
for (i = 0; i < idx; i++)
2020-03-16 02:54:20 +01:00
if (cabsl(z - criticals[i]) < 1.0e-8)
2020-03-07 23:32:57 +01:00
return;
criticals[idx++] = z;
}
size_t
2020-03-09 01:32:30 +01:00
criticals_size(void)
2020-03-07 23:32:57 +01:00
{
return idx;
}
long double complex
2020-03-09 01:32:30 +01:00
criticals_get(size_t i)
2020-03-07 23:32:57 +01:00
{
2020-03-08 22:24:10 +01:00
if (i >= idx)
errx(1, "get_critical: index %zu out of range", i);
2020-03-07 23:32:57 +01:00
return criticals[i];
}