From f9920590bc711f8a6aee5d71348c8ef6b42bed70 Mon Sep 17 00:00:00 2001 From: "B. Bogart" Date: Tue, 30 Sep 2003 16:06:46 +0000 Subject: again, I missed a few. This should now be complete! svn path=/trunk/externals/bbogart/chaos/; revision=1053 --- logistic.c | 247 ++++++++++++++++++++++++++++++++++++++++++ lotka_volterra.c | 325 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lyapunov.c | 90 +++++++++++++++ lyapunov.h | 25 +++++ martin.c | 259 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 946 insertions(+) create mode 100644 logistic.c create mode 100644 lotka_volterra.c create mode 100644 lyapunov.c create mode 100644 lyapunov.h create mode 100644 martin.c diff --git a/logistic.c b/logistic.c new file mode 100644 index 0000000..509bfb3 --- /dev/null +++ b/logistic.c @@ -0,0 +1,247 @@ +/* logistic Attractor PD External */ +/* Copyright Michael McGonagle, from ??????, 2003 */ +/* This program is distributed under the params of the GNU Public License */ + +/////////////////////////////////////////////////////////////////////////////////// +/* This file is part of Chaos PD Externals. */ +/* */ +/* Chaos PD Externals are free software; you can redistribute them and/or modify */ +/* them under the terms of the GNU General Public License as published by */ +/* the Free Software Foundation; either version 2 of the License, or */ +/* (at your option) any later version. */ +/* */ +/* Chaos PD Externals are distributed in the hope that they will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with the Chaos PD Externals; if not, write to the Free Software */ +/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include "lyapunov.h" + +#define M_a_lo 0 +#define M_a_hi 4 + +#define M_a 0 + +#define M_x 0 + +#define M_param_count 1 +#define M_var_count 1 +#define M_search_count 3 +#define M_failure_limit 1000 + +static char *version = "logistic v0.0, by Michael McGonagle, from ??????, 2003"; + +t_class *logistic_class; + +typedef struct logistic_struct { + t_object x_obj; + + double vars[M_var_count]; + double vars_init[M_var_count]; + t_atom vars_out[M_var_count]; + t_outlet *vars_outlet; + + t_atom search_out[M_search_count]; + t_outlet *search_outlet; + + double a, a_lo, a_hi; + t_atom params_out[M_param_count]; + t_outlet *params_outlet; + double lyap_exp, lyap_lo, lyap_hi, lyap_limit, failure_ratio; +} logistic_struct; + +static void calc(logistic_struct *logistic, double *vars) { + double x_0; + x_0 =logistic -> a*vars[M_x]*(1.0-vars[M_x]); + vars[M_x] = x_0; +} // end calc + +static void calculate(logistic_struct *logistic) { + calc(logistic, logistic -> vars); + outlet_float(logistic -> x_obj.ob_outlet, logistic -> vars[M_x]); +} // end calculate + +static void reset(logistic_struct *logistic, t_symbol *s, int argc, t_atom *argv) { + if (argc == M_var_count) { + logistic -> vars[M_x] = (double) atom_getfloatarg(M_x, argc, argv); + } else { + logistic -> vars[M_x] = logistic -> vars_init[M_x]; + } // end if +} // end reset + +static char *classify(logistic_struct *logistic) { + static char buff[2]; + char *c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + buff[0] = c[(int) (((logistic -> a - M_a_lo) * (1.0 / (M_a_hi - M_a_lo))) * 26)]; + buff[1] = '\0'; + return buff; +} + +static void make_results(logistic_struct *logistic) { + SETFLOAT(&logistic -> search_out[0], logistic -> lyap_exp); + SETSYMBOL(&logistic -> search_out[1], gensym(classify(logistic))); + SETFLOAT(&logistic -> search_out[2], logistic -> failure_ratio); + SETFLOAT(&logistic -> vars_out[M_x], logistic -> vars[M_x]); + SETFLOAT(&logistic -> params_out[M_a], logistic -> a); + outlet_list(logistic -> params_outlet, gensym("list"), M_param_count, logistic -> params_out); + outlet_list(logistic -> vars_outlet, gensym("list"), M_var_count, logistic -> vars_out); +} + +static void show(logistic_struct *logistic) { + make_results(logistic); + outlet_anything(logistic -> search_outlet, gensym("show"), M_search_count, logistic -> search_out); +} + +static void param(logistic_struct *logistic, t_symbol *s, int argc, t_atom *argv) { + if (argc != 1) { + post("Incorrect number of arguments for logistic fractal. Expecting 1 arguments."); + return; + } + logistic -> a = (double) atom_getfloatarg(0, argc, argv); +} + +static void seed(logistic_struct *logistic, t_symbol *s, int argc, t_atom *argv) { + if (argc > 0) { + srand48(((unsigned int)time(0))|1); + } else { + srand48((unsigned int) atom_getfloatarg(0, argc, argv)); + } +} + +static void lyap(logistic_struct *logistic, t_floatarg l, t_floatarg h, t_floatarg lim) { + logistic -> lyap_lo = l; + logistic -> lyap_hi = h; + logistic -> lyap_limit = (double) ((int) lim); +} + +static void elyap(logistic_struct *logistic) { + double results[M_var_count]; + int i; + if (lyapunov_full((void *) logistic, (t_gotfn) calc, M_var_count, logistic -> vars, results) != NULL) { + post("elyapunov:"); + for(i = 0; i < M_var_count; i++) { post("%d: %3.80f", i, results[i]); } + } +} + +static void limiter(logistic_struct *logistic) { + if (logistic -> a_lo < M_a_lo) { logistic -> a_lo = M_a_lo; } + if (logistic -> a_lo > M_a_hi) { logistic -> a_lo = M_a_hi; } + if (logistic -> a_hi < M_a_lo) { logistic -> a_hi = M_a_lo; } + if (logistic -> a_hi > M_a_hi) { logistic -> a_hi = M_a_hi; } +} + +static void constrain(logistic_struct *logistic, t_symbol *s, int argc, t_atom *argv) { + int i; + t_atom *arg = argv; + if (argc == 0) { + // reset to full limits of search ranges + logistic -> a_lo = M_a_lo; + logistic -> a_hi = M_a_hi; + return; + } + if (argc == 1) { + // set the ranges based on percentage of full range + double percent = atom_getfloat(arg); + double a_spread = ((M_a_hi - M_a_lo) * percent) / 2; + logistic -> a_lo = logistic -> a - a_spread; + logistic -> a_hi = logistic -> a + a_spread; + limiter(logistic); + return; + } + if (argc != M_param_count * 2) { + post("Invalid number of arguments for logistic constraints, requires 2 values, got %d", argc); + return; + } + logistic -> a_lo = atom_getfloat(arg++); + logistic -> a_hi = atom_getfloat(arg++); + limiter(logistic); +} + +static void search(logistic_struct *logistic, t_symbol *s, int argc, t_atom *argv) { + int not_found, not_expired = logistic -> lyap_limit; + int jump, i, iterations; + t_atom vars[M_var_count]; + double temp_a = logistic -> a; + if (argc > 0) { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], atom_getfloatarg(i, argc, argv)); + } + } else { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], logistic -> vars_init[i]); + } + } + do { + jump = 500; + not_found = 0; + iterations = 10000; + bad_params: + logistic -> a = (drand48() * (logistic -> a_hi - logistic -> a_lo)) + logistic -> a_lo; + // put any preliminary checks specific to this fractal to eliminate bad_params + + reset(logistic, NULL, argc, vars); + do { calc(logistic, logistic -> vars); } while(jump--); + logistic -> lyap_exp = lyapunov((void *) logistic, (t_gotfn) calc, M_var_count, (double *) logistic -> vars); + if (isnan(logistic -> lyap_exp)) { not_found = 1; } + if (logistic -> lyap_exp < logistic -> lyap_lo || logistic -> lyap_exp > logistic -> lyap_hi) { not_found = 1; } + not_expired--; + } while(not_found && not_expired); + reset(logistic, NULL, argc, vars); + if (!not_expired) { + post("Could not find a fractal after %d attempts.", (int) logistic -> lyap_limit); + post("Try using wider constraints."); + logistic -> a = temp_a; + outlet_anything(logistic -> search_outlet, gensym("invalid"), 0, NULL); + } else { + logistic -> failure_ratio = (logistic -> lyap_limit - not_expired) / logistic -> lyap_limit; + make_results(logistic); + outlet_anything(logistic -> search_outlet, gensym("search"), M_search_count, logistic -> search_out); + } +} + +void *logistic_new(t_symbol *s, int argc, t_atom *argv) { + logistic_struct *logistic = (logistic_struct *) pd_new(logistic_class); + if (logistic != NULL) { + outlet_new(&logistic -> x_obj, &s_float); + logistic -> search_outlet = outlet_new(&logistic -> x_obj, &s_list); + logistic -> vars_outlet = outlet_new(&logistic -> x_obj, &s_list); + logistic -> params_outlet = outlet_new(&logistic -> x_obj, &s_list); + if (argc == M_param_count + M_var_count) { + logistic -> vars_init[M_x] = logistic -> vars[M_x] = (double) atom_getfloatarg(0, argc, argv); + logistic -> a = (double) atom_getfloatarg(1, argc, argv); + } else { + if (argc != 0 && argc != M_param_count + M_var_count) { + post("Incorrect number of arguments for logistic fractal. Expecting 2 arguments."); + } + logistic -> vars_init[M_x] = 0.1; + logistic -> a = 4; + } + constrain(logistic, NULL, 0, NULL); + lyap(logistic, -1000000.0, 1000000.0, M_failure_limit); + } + return (void *)logistic; +} + +void logistic_setup(void) { + logistic_class = class_new(gensym("logistic"), (t_newmethod) logistic_new, 0, sizeof(logistic_struct), 0, A_GIMME, 0); + class_addbang(logistic_class, (t_method) calculate); + class_addmethod(logistic_class, (t_method) reset, gensym("reset"), A_GIMME, 0); + class_addmethod(logistic_class, (t_method) show, gensym("show"), 0); + class_addmethod(logistic_class, (t_method) param, gensym("param"), A_GIMME, 0); + class_addmethod(logistic_class, (t_method) seed, gensym("seed"), A_GIMME, 0); + class_addmethod(logistic_class, (t_method) lyap, gensym("lyapunov"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + class_addmethod(logistic_class, (t_method) elyap, gensym("elyapunov"), 0); + class_addmethod(logistic_class, (t_method) search, gensym("search"), A_GIMME, 0); + class_addmethod(logistic_class, (t_method) constrain, gensym("constrain"), A_GIMME, 0); + class_sethelpsymbol(logistic_class, gensym("help-logistic.pd")); +} + diff --git a/lotka_volterra.c b/lotka_volterra.c new file mode 100644 index 0000000..ecb4b75 --- /dev/null +++ b/lotka_volterra.c @@ -0,0 +1,325 @@ +/* lotkavolterra Attractor PD External */ +/* Copyright Michael McGonagle, 2003 */ +/* This program is distributed under the params of the GNU Public License */ + +/////////////////////////////////////////////////////////////////////////////////// +/* This file is part of Chaos PD Externals. */ +/* */ +/* Chaos PD Externals are free software; you can redistribute them and/or modify */ +/* them under the terms of the GNU General Public License as published by */ +/* the Free Software Foundation; either version 2 of the License, or */ +/* (at your option) any later version. */ +/* */ +/* Chaos PD Externals are distributed in the hope that they will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with the Chaos PD Externals; if not, write to the Free Software */ +/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include "lyapunov.h" + +#define M_a_lo -1000 +#define M_a_hi 1000 +#define M_b_lo -1000 +#define M_b_hi 1000 +#define M_c_lo -1000 +#define M_c_hi 1000 +#define M_e_lo -1000 +#define M_e_hi 1000 + +#define M_a 0 +#define M_b 1 +#define M_c 2 +#define M_e 3 + +#define M_r 0 +#define M_f 1 + +#define M_param_count 4 +#define M_var_count 2 +#define M_search_count 3 +#define M_failure_limit 1000 + +static char *version = "lotkavolterra v0.0, by Michael McGonagle, 2003"; + +t_class *lotkavolterra_class; + +typedef struct lotkavolterra_struct { + t_object x_obj; + + double vars[M_var_count]; + double vars_init[M_var_count]; + t_atom vars_out[M_var_count]; + t_outlet *vars_outlet; + + t_atom search_out[M_search_count]; + t_outlet *search_outlet; + + double a, a_lo, a_hi, b, b_lo, b_hi, c, c_lo, c_hi, e, e_lo, e_hi; + t_atom params_out[M_param_count]; + t_outlet *params_outlet; + double lyap_exp, lyap_lo, lyap_hi, lyap_limit, failure_ratio; + + t_outlet *outlets[M_var_count - 1]; +} lotkavolterra_struct; + +static void calc(lotkavolterra_struct *lotkavolterra, double *vars) { + double r_0, f_0; + r_0 =vars[M_r]+lotkavolterra -> a*vars[M_r]-lotkavolterra -> b*vars[M_r]*vars[M_f]; + f_0 =vars[M_f]+lotkavolterra -> e*lotkavolterra -> b*vars[M_r]*vars[M_f]-lotkavolterra -> c*vars[M_f]; + vars[M_r] = r_0; + vars[M_f] = f_0; +} // end calc + +static void calculate(lotkavolterra_struct *lotkavolterra) { + calc(lotkavolterra, lotkavolterra -> vars); + outlet_float(lotkavolterra -> x_obj.ob_outlet, lotkavolterra -> vars[M_r]); + outlet_float(lotkavolterra -> outlets[M_f - 1], lotkavolterra -> vars[M_f]); +} // end calculate + +static void reset(lotkavolterra_struct *lotkavolterra, t_symbol *s, int argc, t_atom *argv) { + if (argc == M_var_count) { + lotkavolterra -> vars[M_r] = (double) atom_getfloatarg(M_r, argc, argv); + lotkavolterra -> vars[M_f] = (double) atom_getfloatarg(M_f, argc, argv); + } else { + lotkavolterra -> vars[M_r] = lotkavolterra -> vars_init[M_r]; + lotkavolterra -> vars[M_f] = lotkavolterra -> vars_init[M_f]; + } // end if +} // end reset + +static char *classify(lotkavolterra_struct *lotkavolterra) { + static char buff[5]; + char *c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + buff[0] = c[(int) (((lotkavolterra -> a - M_a_lo) * (1.0 / (M_a_hi - M_a_lo))) * 26)]; + buff[1] = c[(int) (((lotkavolterra -> b - M_b_lo) * (1.0 / (M_b_hi - M_b_lo))) * 26)]; + buff[2] = c[(int) (((lotkavolterra -> c - M_c_lo) * (1.0 / (M_c_hi - M_c_lo))) * 26)]; + buff[3] = c[(int) (((lotkavolterra -> e - M_e_lo) * (1.0 / (M_e_hi - M_e_lo))) * 26)]; + buff[4] = '\0'; + return buff; +} + +static void make_results(lotkavolterra_struct *lotkavolterra) { + SETFLOAT(&lotkavolterra -> search_out[0], lotkavolterra -> lyap_exp); + SETSYMBOL(&lotkavolterra -> search_out[1], gensym(classify(lotkavolterra))); + SETFLOAT(&lotkavolterra -> search_out[2], lotkavolterra -> failure_ratio); + SETFLOAT(&lotkavolterra -> vars_out[M_r], lotkavolterra -> vars[M_r]); + SETFLOAT(&lotkavolterra -> vars_out[M_f], lotkavolterra -> vars[M_f]); + SETFLOAT(&lotkavolterra -> params_out[M_a], lotkavolterra -> a); + SETFLOAT(&lotkavolterra -> params_out[M_b], lotkavolterra -> b); + SETFLOAT(&lotkavolterra -> params_out[M_c], lotkavolterra -> c); + SETFLOAT(&lotkavolterra -> params_out[M_e], lotkavolterra -> e); + outlet_list(lotkavolterra -> params_outlet, gensym("list"), M_param_count, lotkavolterra -> params_out); + outlet_list(lotkavolterra -> vars_outlet, gensym("list"), M_var_count, lotkavolterra -> vars_out); +} + +static void show(lotkavolterra_struct *lotkavolterra) { + make_results(lotkavolterra); + outlet_anything(lotkavolterra -> search_outlet, gensym("show"), M_search_count, lotkavolterra -> search_out); +} + +static void param(lotkavolterra_struct *lotkavolterra, t_symbol *s, int argc, t_atom *argv) { + if (argc != 4) { + post("Incorrect number of arguments for lotkavolterra fractal. Expecting 4 arguments."); + return; + } + lotkavolterra -> a = (double) atom_getfloatarg(0, argc, argv); + lotkavolterra -> b = (double) atom_getfloatarg(1, argc, argv); + lotkavolterra -> c = (double) atom_getfloatarg(2, argc, argv); + lotkavolterra -> e = (double) atom_getfloatarg(3, argc, argv); +} + +static void seed(lotkavolterra_struct *lotkavolterra, t_symbol *s, int argc, t_atom *argv) { + if (argc > 0) { + srand48(((unsigned int)time(0))|1); + } else { + srand48((unsigned int) atom_getfloatarg(0, argc, argv)); + } +} + +static void lyap(lotkavolterra_struct *lotkavolterra, t_floatarg l, t_floatarg h, t_floatarg lim) { + lotkavolterra -> lyap_lo = l; + lotkavolterra -> lyap_hi = h; + lotkavolterra -> lyap_limit = (double) ((int) lim); +} + +static void elyap(lotkavolterra_struct *lotkavolterra) { + double results[M_var_count]; + int i; + if (lyapunov_full((void *) lotkavolterra, (t_gotfn) calc, M_var_count, lotkavolterra -> vars, results) != NULL) { + post("elyapunov:"); + for(i = 0; i < M_var_count; i++) { post("%d: %3.80f", i, results[i]); } + } +} + +static void limiter(lotkavolterra_struct *lotkavolterra) { + if (lotkavolterra -> a_lo < M_a_lo) { lotkavolterra -> a_lo = M_a_lo; } + if (lotkavolterra -> a_lo > M_a_hi) { lotkavolterra -> a_lo = M_a_hi; } + if (lotkavolterra -> a_hi < M_a_lo) { lotkavolterra -> a_hi = M_a_lo; } + if (lotkavolterra -> a_hi > M_a_hi) { lotkavolterra -> a_hi = M_a_hi; } + if (lotkavolterra -> b_lo < M_b_lo) { lotkavolterra -> b_lo = M_b_lo; } + if (lotkavolterra -> b_lo > M_b_hi) { lotkavolterra -> b_lo = M_b_hi; } + if (lotkavolterra -> b_hi < M_b_lo) { lotkavolterra -> b_hi = M_b_lo; } + if (lotkavolterra -> b_hi > M_b_hi) { lotkavolterra -> b_hi = M_b_hi; } + if (lotkavolterra -> c_lo < M_c_lo) { lotkavolterra -> c_lo = M_c_lo; } + if (lotkavolterra -> c_lo > M_c_hi) { lotkavolterra -> c_lo = M_c_hi; } + if (lotkavolterra -> c_hi < M_c_lo) { lotkavolterra -> c_hi = M_c_lo; } + if (lotkavolterra -> c_hi > M_c_hi) { lotkavolterra -> c_hi = M_c_hi; } + if (lotkavolterra -> e_lo < M_e_lo) { lotkavolterra -> e_lo = M_e_lo; } + if (lotkavolterra -> e_lo > M_e_hi) { lotkavolterra -> e_lo = M_e_hi; } + if (lotkavolterra -> e_hi < M_e_lo) { lotkavolterra -> e_hi = M_e_lo; } + if (lotkavolterra -> e_hi > M_e_hi) { lotkavolterra -> e_hi = M_e_hi; } +} + +static void constrain(lotkavolterra_struct *lotkavolterra, t_symbol *s, int argc, t_atom *argv) { + int i; + t_atom *arg = argv; + if (argc == 0) { + // reset to full limits of search ranges + lotkavolterra -> a_lo = M_a_lo; + lotkavolterra -> a_hi = M_a_hi; + lotkavolterra -> b_lo = M_b_lo; + lotkavolterra -> b_hi = M_b_hi; + lotkavolterra -> c_lo = M_c_lo; + lotkavolterra -> c_hi = M_c_hi; + lotkavolterra -> e_lo = M_e_lo; + lotkavolterra -> e_hi = M_e_hi; + return; + } + if (argc == 1) { + // set the ranges based on percentage of full range + double percent = atom_getfloat(arg); + double a_spread = ((M_a_hi - M_a_lo) * percent) / 2; + double b_spread = ((M_b_hi - M_b_lo) * percent) / 2; + double c_spread = ((M_c_hi - M_c_lo) * percent) / 2; + double e_spread = ((M_e_hi - M_e_lo) * percent) / 2; + lotkavolterra -> a_lo = lotkavolterra -> a - a_spread; + lotkavolterra -> a_hi = lotkavolterra -> a + a_spread; + lotkavolterra -> b_lo = lotkavolterra -> b - b_spread; + lotkavolterra -> b_hi = lotkavolterra -> b + b_spread; + lotkavolterra -> c_lo = lotkavolterra -> c - c_spread; + lotkavolterra -> c_hi = lotkavolterra -> c + c_spread; + lotkavolterra -> e_lo = lotkavolterra -> e - e_spread; + lotkavolterra -> e_hi = lotkavolterra -> e + e_spread; + limiter(lotkavolterra); + return; + } + if (argc != M_param_count * 2) { + post("Invalid number of arguments for lotkavolterra constraints, requires 8 values, got %d", argc); + return; + } + lotkavolterra -> a_lo = atom_getfloat(arg++); + lotkavolterra -> a_hi = atom_getfloat(arg++); + lotkavolterra -> b_lo = atom_getfloat(arg++); + lotkavolterra -> b_hi = atom_getfloat(arg++); + lotkavolterra -> c_lo = atom_getfloat(arg++); + lotkavolterra -> c_hi = atom_getfloat(arg++); + lotkavolterra -> e_lo = atom_getfloat(arg++); + lotkavolterra -> e_hi = atom_getfloat(arg++); + limiter(lotkavolterra); +} + +static void search(lotkavolterra_struct *lotkavolterra, t_symbol *s, int argc, t_atom *argv) { + int not_found, not_expired = lotkavolterra -> lyap_limit; + int jump, i, iterations; + t_atom vars[M_var_count]; + double temp_a = lotkavolterra -> a; + double temp_b = lotkavolterra -> b; + double temp_c = lotkavolterra -> c; + double temp_e = lotkavolterra -> e; + if (argc > 0) { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], atom_getfloatarg(i, argc, argv)); + } + } else { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], lotkavolterra -> vars_init[i]); + } + } + do { + jump = 500; + not_found = 0; + iterations = 10000; + bad_params: + lotkavolterra -> a = (drand48() * (lotkavolterra -> a_hi - lotkavolterra -> a_lo)) + lotkavolterra -> a_lo; + lotkavolterra -> b = (drand48() * (lotkavolterra -> b_hi - lotkavolterra -> b_lo)) + lotkavolterra -> b_lo; + lotkavolterra -> c = (drand48() * (lotkavolterra -> c_hi - lotkavolterra -> c_lo)) + lotkavolterra -> c_lo; + lotkavolterra -> e = (drand48() * (lotkavolterra -> e_hi - lotkavolterra -> e_lo)) + lotkavolterra -> e_lo; + // put any preliminary checks specific to this fractal to eliminate bad_params + + reset(lotkavolterra, NULL, argc, vars); + do { calc(lotkavolterra, lotkavolterra -> vars); } while(jump--); + lotkavolterra -> lyap_exp = lyapunov((void *) lotkavolterra, (t_gotfn) calc, M_var_count, (double *) lotkavolterra -> vars); + if (isnan(lotkavolterra -> lyap_exp)) { not_found = 1; } + if (lotkavolterra -> lyap_exp < lotkavolterra -> lyap_lo || lotkavolterra -> lyap_exp > lotkavolterra -> lyap_hi) { not_found = 1; } + not_expired--; + } while(not_found && not_expired); + reset(lotkavolterra, NULL, argc, vars); + if (!not_expired) { + post("Could not find a fractal after %d attempts.", (int) lotkavolterra -> lyap_limit); + post("Try using wider constraints."); + lotkavolterra -> a = temp_a; + lotkavolterra -> b = temp_b; + lotkavolterra -> c = temp_c; + lotkavolterra -> e = temp_e; + outlet_anything(lotkavolterra -> search_outlet, gensym("invalid"), 0, NULL); + } else { + lotkavolterra -> failure_ratio = (lotkavolterra -> lyap_limit - not_expired) / lotkavolterra -> lyap_limit; + make_results(lotkavolterra); + outlet_anything(lotkavolterra -> search_outlet, gensym("search"), M_search_count, lotkavolterra -> search_out); + } +} + +void *lotkavolterra_new(t_symbol *s, int argc, t_atom *argv) { + lotkavolterra_struct *lotkavolterra = (lotkavolterra_struct *) pd_new(lotkavolterra_class); + if (lotkavolterra != NULL) { + outlet_new(&lotkavolterra -> x_obj, &s_float); + lotkavolterra -> outlets[0] = outlet_new(&lotkavolterra -> x_obj, &s_float); + lotkavolterra -> search_outlet = outlet_new(&lotkavolterra -> x_obj, &s_list); + lotkavolterra -> vars_outlet = outlet_new(&lotkavolterra -> x_obj, &s_list); + lotkavolterra -> params_outlet = outlet_new(&lotkavolterra -> x_obj, &s_list); + if (argc == M_param_count + M_var_count) { + lotkavolterra -> vars_init[M_r] = lotkavolterra -> vars[M_r] = (double) atom_getfloatarg(0, argc, argv); + lotkavolterra -> vars_init[M_f] = lotkavolterra -> vars[M_f] = (double) atom_getfloatarg(1, argc, argv); + lotkavolterra -> a = (double) atom_getfloatarg(2, argc, argv); + lotkavolterra -> b = (double) atom_getfloatarg(3, argc, argv); + lotkavolterra -> c = (double) atom_getfloatarg(4, argc, argv); + lotkavolterra -> e = (double) atom_getfloatarg(5, argc, argv); + } else { + if (argc != 0 && argc != M_param_count + M_var_count) { + post("Incorrect number of arguments for lotkavolterra fractal. Expecting 6 arguments."); + } + lotkavolterra -> vars_init[M_r] = 0.1; + lotkavolterra -> vars_init[M_f] = 0.1; + lotkavolterra -> a = 0.04; + lotkavolterra -> b = 0.0005; + lotkavolterra -> c = 0.2; + lotkavolterra -> e = 0.1; + } + constrain(lotkavolterra, NULL, 0, NULL); + lyap(lotkavolterra, -1000000.0, 1000000.0, M_failure_limit); + } + return (void *)lotkavolterra; +} + +void lotkavolterra_setup(void) { + lotkavolterra_class = class_new(gensym("lotkavolterra"), (t_newmethod) lotkavolterra_new, 0, sizeof(lotkavolterra_struct), 0, A_GIMME, 0); + class_addbang(lotkavolterra_class, (t_method) calculate); + class_addmethod(lotkavolterra_class, (t_method) reset, gensym("reset"), A_GIMME, 0); + class_addmethod(lotkavolterra_class, (t_method) show, gensym("show"), 0); + class_addmethod(lotkavolterra_class, (t_method) param, gensym("param"), A_GIMME, 0); + class_addmethod(lotkavolterra_class, (t_method) seed, gensym("seed"), A_GIMME, 0); + class_addmethod(lotkavolterra_class, (t_method) lyap, gensym("lyapunov"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + class_addmethod(lotkavolterra_class, (t_method) elyap, gensym("elyapunov"), 0); + class_addmethod(lotkavolterra_class, (t_method) search, gensym("search"), A_GIMME, 0); + class_addmethod(lotkavolterra_class, (t_method) constrain, gensym("constrain"), A_GIMME, 0); + class_sethelpsymbol(lotkavolterra_class, gensym("help-lotkavolterra.pd")); +} + diff --git a/lyapunov.c b/lyapunov.c new file mode 100644 index 0000000..04153ba --- /dev/null +++ b/lyapunov.c @@ -0,0 +1,90 @@ +#include +#include +#include "lyapunov.h" + +#define LY_ITERATIONS 50000 + +//#define LY_ABERATION 1e-6 +#define LY_ABERATION 10e-15 + +/** + * this expects the fractal to already be stable (ie iterate it a few hundred/thousand times). + + Steps as described by Julian Sprott + 1. Start with any initial condition in the basin of attraction + 2. Iterate until the orbit is on the attractor + 3. Select (almost any) nearby point (separated by d0) + 4. Advancce both orbits one iteration and calculate the new separation d1 + 5. Evaluate log(d1/d0) in any convienient base + 6. Readjust one orbit so its separation is d0 in the same direction as d1 + 7. Repeat steps 4-6 many times and calculate the average of step 5 + */ +double lyapunov_eval(void *fractal, t_gotfn calc, int var_count, double *vars, double *test) { + int i, j; + double exponent = 0.0, sum = 0.0, d2, df, rs; + double diff[MAX_VARS]; + + for(i = 0; i < LY_ITERATIONS; i++) { + // 4. iterate each set + calc(fractal, vars); + calc(fractal, test); + // 5. Evaluate + d2 = 0.0; + for(j = 0; j < var_count; j++) { + diff[j] = test[j] - vars[j]; + //fprintf(stderr, "vars[%d] = %0.30f\n test[%d] = %0.30f\n diff[%d] = %0.30f\n", + // j, vars[j], j, test[j], j, diff[j]); + d2 += diff[j] * diff[j]; + } + df = 1000000000000.0 * d2; + rs = 1.0 / sqrt(df); + sum += log(df); + exponent = 0.721347 * sum / i; + //fprintf(stderr, "%d\n d2 = %0.30f\n df = %0.30f\n rs = %0.30f\n sum = %0.30f\nexponent = %0.30f\n\n", + // i, d2, df, rs, sum, exponent); + // 6. adjust the orbit under test + for(j = 0; j < var_count; j++) { + test[j] = vars[j] + (rs * (test[j] - vars[j])); + } + } + return exponent; +} + +double lyapunov(void *fractal, t_gotfn calc, int var_count, double *vars) { + int i; + double test[MAX_VARS]; + + // 1. + 2. 'vars' is expected to be ready to start computing + // 3. create a test set of variables + test[0] = vars[0] + LY_ABERATION; + for(i = 1; i < var_count; i++) { test[i] = vars[i]; } + + return lyapunov_eval(fractal, calc, var_count, vars, test); +} + +double *lyapunov_full(void *fractal, t_gotfn calc, int var_count, double *vars, double *results) { + int i, j; + double initial[MAX_VARS]; + double test[MAX_VARS]; + + // save the starting values + for(i = 0; i < var_count; i++) { + initial[i] = vars[i]; + } + for(i = 0; i < var_count; i++) { + // aberate each variable in turn + for(j = 0; j < var_count; j++) { + if (j == i) { + test[j] = vars[j] + LY_ABERATION; + } else { + test[j] = vars[j]; + } + } + results[i] = lyapunov_eval(fractal, calc, var_count, vars, test); + // set the vars back the initial values + for(j = 0; j < var_count; j++) { + vars[j] = initial[j]; + } + } + return results; +} diff --git a/lyapunov.h b/lyapunov.h new file mode 100644 index 0000000..d6b71f5 --- /dev/null +++ b/lyapunov.h @@ -0,0 +1,25 @@ +/* + a test file for lyapunov.h + */ + +#ifndef PD_VERSION +#include "m_pd.h" +#endif + +#define MAX_VARS 20 + +/* + * fractal - pointer to the object under test + * calc - the iteration function + * var_count - the number of variables for this fractal class + * vars - pointer to the fractal variables array + */ +double lyapunov(void *fractal, t_gotfn calc, int var_count, double *vars); + +/* + * CAUTION: ON FRACTALS WITH LOTS OF VARIABLES, THIS WILL TAKE A WHILE. + * This is an experimental function to test the ability of the function to + * calculate an accurate exponent by aberating each variable in the fractal. + * returns 'results' on completion. + */ +double *lyapunov_full(void *fractal, t_gotfn calc, int var_count, double *vars, double *results); diff --git a/martin.c b/martin.c new file mode 100644 index 0000000..bc8b6ff --- /dev/null +++ b/martin.c @@ -0,0 +1,259 @@ +/* martin Attractor PD External */ +/* Copyright Michael McGonagle, from ??????, 2003 */ +/* This program is distributed under the params of the GNU Public License */ + +/////////////////////////////////////////////////////////////////////////////////// +/* This file is part of Chaos PD Externals. */ +/* */ +/* Chaos PD Externals are free software; you can redistribute them and/or modify */ +/* them under the terms of the GNU General Public License as published by */ +/* the Free Software Foundation; either version 2 of the License, or */ +/* (at your option) any later version. */ +/* */ +/* Chaos PD Externals are distributed in the hope that they will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with the Chaos PD Externals; if not, write to the Free Software */ +/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include "lyapunov.h" + +#define M_a_lo 0 +#define M_a_hi 1000 + +#define M_a 0 + +#define M_x 0 +#define M_y 1 + +#define M_param_count 1 +#define M_var_count 2 +#define M_search_count 3 +#define M_failure_limit 1000 + +static char *version = "martin v0.0, by Michael McGonagle, from ??????, 2003"; + +t_class *martin_class; + +typedef struct martin_struct { + t_object x_obj; + + double vars[M_var_count]; + double vars_init[M_var_count]; + t_atom vars_out[M_var_count]; + t_outlet *vars_outlet; + + t_atom search_out[M_search_count]; + t_outlet *search_outlet; + + double a, a_lo, a_hi; + t_atom params_out[M_param_count]; + t_outlet *params_outlet; + double lyap_exp, lyap_lo, lyap_hi, lyap_limit, failure_ratio; + + t_outlet *outlets[M_var_count - 1]; +} martin_struct; + +static void calc(martin_struct *martin, double *vars) { + double x_0, y_0; + x_0 =vars[M_y]-sin(vars[M_x]); + y_0 =martin -> a-vars[M_x]; + vars[M_x] = x_0; + vars[M_y] = y_0; +} // end calc + +static void calculate(martin_struct *martin) { + calc(martin, martin -> vars); + outlet_float(martin -> x_obj.ob_outlet, martin -> vars[M_x]); + outlet_float(martin -> outlets[M_y - 1], martin -> vars[M_y]); +} // end calculate + +static void reset(martin_struct *martin, t_symbol *s, int argc, t_atom *argv) { + if (argc == M_var_count) { + martin -> vars[M_x] = (double) atom_getfloatarg(M_x, argc, argv); + martin -> vars[M_y] = (double) atom_getfloatarg(M_y, argc, argv); + } else { + martin -> vars[M_x] = martin -> vars_init[M_x]; + martin -> vars[M_y] = martin -> vars_init[M_y]; + } // end if +} // end reset + +static char *classify(martin_struct *martin) { + static char buff[2]; + char *c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + buff[0] = c[(int) (((martin -> a - M_a_lo) * (1.0 / (M_a_hi - M_a_lo))) * 26)]; + buff[1] = '\0'; + return buff; +} + +static void make_results(martin_struct *martin) { + SETFLOAT(&martin -> search_out[0], martin -> lyap_exp); + SETSYMBOL(&martin -> search_out[1], gensym(classify(martin))); + SETFLOAT(&martin -> search_out[2], martin -> failure_ratio); + SETFLOAT(&martin -> vars_out[M_x], martin -> vars[M_x]); + SETFLOAT(&martin -> vars_out[M_y], martin -> vars[M_y]); + SETFLOAT(&martin -> params_out[M_a], martin -> a); + outlet_list(martin -> params_outlet, gensym("list"), M_param_count, martin -> params_out); + outlet_list(martin -> vars_outlet, gensym("list"), M_var_count, martin -> vars_out); +} + +static void show(martin_struct *martin) { + make_results(martin); + outlet_anything(martin -> search_outlet, gensym("show"), M_search_count, martin -> search_out); +} + +static void param(martin_struct *martin, t_symbol *s, int argc, t_atom *argv) { + if (argc != 1) { + post("Incorrect number of arguments for martin fractal. Expecting 1 arguments."); + return; + } + martin -> a = (double) atom_getfloatarg(0, argc, argv); +} + +static void seed(martin_struct *martin, t_symbol *s, int argc, t_atom *argv) { + if (argc > 0) { + srand48(((unsigned int)time(0))|1); + } else { + srand48((unsigned int) atom_getfloatarg(0, argc, argv)); + } +} + +static void lyap(martin_struct *martin, t_floatarg l, t_floatarg h, t_floatarg lim) { + martin -> lyap_lo = l; + martin -> lyap_hi = h; + martin -> lyap_limit = (double) ((int) lim); +} + +static void elyap(martin_struct *martin) { + double results[M_var_count]; + int i; + if (lyapunov_full((void *) martin, (t_gotfn) calc, M_var_count, martin -> vars, results) != NULL) { + post("elyapunov:"); + for(i = 0; i < M_var_count; i++) { post("%d: %3.80f", i, results[i]); } + } +} + +static void limiter(martin_struct *martin) { + if (martin -> a_lo < M_a_lo) { martin -> a_lo = M_a_lo; } + if (martin -> a_lo > M_a_hi) { martin -> a_lo = M_a_hi; } + if (martin -> a_hi < M_a_lo) { martin -> a_hi = M_a_lo; } + if (martin -> a_hi > M_a_hi) { martin -> a_hi = M_a_hi; } +} + +static void constrain(martin_struct *martin, t_symbol *s, int argc, t_atom *argv) { + int i; + t_atom *arg = argv; + if (argc == 0) { + // reset to full limits of search ranges + martin -> a_lo = M_a_lo; + martin -> a_hi = M_a_hi; + return; + } + if (argc == 1) { + // set the ranges based on percentage of full range + double percent = atom_getfloat(arg); + double a_spread = ((M_a_hi - M_a_lo) * percent) / 2; + martin -> a_lo = martin -> a - a_spread; + martin -> a_hi = martin -> a + a_spread; + limiter(martin); + return; + } + if (argc != M_param_count * 2) { + post("Invalid number of arguments for martin constraints, requires 2 values, got %d", argc); + return; + } + martin -> a_lo = atom_getfloat(arg++); + martin -> a_hi = atom_getfloat(arg++); + limiter(martin); +} + +static void search(martin_struct *martin, t_symbol *s, int argc, t_atom *argv) { + int not_found, not_expired = martin -> lyap_limit; + int jump, i, iterations; + t_atom vars[M_var_count]; + double temp_a = martin -> a; + if (argc > 0) { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], atom_getfloatarg(i, argc, argv)); + } + } else { + for (i = 0; i < M_var_count; i++) { + SETFLOAT(&vars[i], martin -> vars_init[i]); + } + } + do { + jump = 500; + not_found = 0; + iterations = 10000; + bad_params: + martin -> a = (drand48() * (martin -> a_hi - martin -> a_lo)) + martin -> a_lo; + // put any preliminary checks specific to this fractal to eliminate bad_params + + reset(martin, NULL, argc, vars); + do { calc(martin, martin -> vars); } while(jump--); + martin -> lyap_exp = lyapunov((void *) martin, (t_gotfn) calc, M_var_count, (double *) martin -> vars); + if (isnan(martin -> lyap_exp)) { not_found = 1; } + if (martin -> lyap_exp < martin -> lyap_lo || martin -> lyap_exp > martin -> lyap_hi) { not_found = 1; } + not_expired--; + } while(not_found && not_expired); + reset(martin, NULL, argc, vars); + if (!not_expired) { + post("Could not find a fractal after %d attempts.", (int) martin -> lyap_limit); + post("Try using wider constraints."); + martin -> a = temp_a; + outlet_anything(martin -> search_outlet, gensym("invalid"), 0, NULL); + } else { + martin -> failure_ratio = (martin -> lyap_limit - not_expired) / martin -> lyap_limit; + make_results(martin); + outlet_anything(martin -> search_outlet, gensym("search"), M_search_count, martin -> search_out); + } +} + +void *martin_new(t_symbol *s, int argc, t_atom *argv) { + martin_struct *martin = (martin_struct *) pd_new(martin_class); + if (martin != NULL) { + outlet_new(&martin -> x_obj, &s_float); + martin -> outlets[0] = outlet_new(&martin -> x_obj, &s_float); + martin -> search_outlet = outlet_new(&martin -> x_obj, &s_list); + martin -> vars_outlet = outlet_new(&martin -> x_obj, &s_list); + martin -> params_outlet = outlet_new(&martin -> x_obj, &s_list); + if (argc == M_param_count + M_var_count) { + martin -> vars_init[M_x] = martin -> vars[M_x] = (double) atom_getfloatarg(0, argc, argv); + martin -> vars_init[M_y] = martin -> vars[M_y] = (double) atom_getfloatarg(1, argc, argv); + martin -> a = (double) atom_getfloatarg(2, argc, argv); + } else { + if (argc != 0 && argc != M_param_count + M_var_count) { + post("Incorrect number of arguments for martin fractal. Expecting 3 arguments."); + } + martin -> vars_init[M_x] = 0.01; + martin -> vars_init[M_y] = 0; + martin -> a = 3.14; + } + constrain(martin, NULL, 0, NULL); + lyap(martin, -1000000.0, 1000000.0, M_failure_limit); + } + return (void *)martin; +} + +void martin_setup(void) { + martin_class = class_new(gensym("martin"), (t_newmethod) martin_new, 0, sizeof(martin_struct), 0, A_GIMME, 0); + class_addbang(martin_class, (t_method) calculate); + class_addmethod(martin_class, (t_method) reset, gensym("reset"), A_GIMME, 0); + class_addmethod(martin_class, (t_method) show, gensym("show"), 0); + class_addmethod(martin_class, (t_method) param, gensym("param"), A_GIMME, 0); + class_addmethod(martin_class, (t_method) seed, gensym("seed"), A_GIMME, 0); + class_addmethod(martin_class, (t_method) lyap, gensym("lyapunov"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + class_addmethod(martin_class, (t_method) elyap, gensym("elyapunov"), 0); + class_addmethod(martin_class, (t_method) search, gensym("search"), A_GIMME, 0); + class_addmethod(martin_class, (t_method) constrain, gensym("constrain"), A_GIMME, 0); + class_sethelpsymbol(martin_class, gensym("help-martin.pd")); +} + -- cgit v1.2.1