Skip to content

Commit

Permalink
A lot of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Dec 22, 2008
1 parent 5d57820 commit 0d37c2a
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 193 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Currently requires Perl 5.10, Math::GSL, JSON, HTML::Template
70 changes: 43 additions & 27 deletions cgi/gsl.cgi
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
#!/usr/bin/perl -w
#!/usr/bin/env perl5.10
# Read-only RESTful API to GSL Special Functions
# Jonathan "Duke" Leto <jonathan@leto.net> - Nov 2008
use strict;
use warnings;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use Scalar::Util qw/reftype/;
use lib '/Library/Perl/5.8.8';
use Scalar::Util qw/reftype/;
use Math::GSL::SF qw/:all/;
use File::Slurp qw/slurp/;
use Regexp::Common qw/number/;
use Math::GSL::Errno qw/:all/;
use HTML::Template;
use JSON;
$|++;

BEGIN { gsl_set_error_handler_off() }
BEGIN { gsl_set_error_handler_off() }

$ENV{REQUEST_METHOD} = 'GET' unless defined $ENV{REQUEST_METHOD};
my %WHITELIST = map { $_ => 1 }
grep { $_ !~ /GSL|_e|_array|INCORRECT/ }
my %WHITELIST = map { $_ => 1 }
grep { $_ !~ /GSL|_e|_array|INCORRECT/ }
sort @Math::GSL::SF::EXPORT_OK ;

my $base_url = 'http://leto.net/rest/gsl.cgi';
my $q = CGI->new;
my $hal = slurp 'tmpl/hal.tmpl';
my $func_select = slurp 'tmpl/gsl_sf_functions.tmpl';
# add support of for _e-only functions
map { $WHITELIST{'gsl_sf_' .$_ } = 1 } qw/exp erf erf_Q erf_Z/ ;

my $base_url = 'http://localhost/~leto/hypatia/cgi/gsl.cgi';
my $q = CGI->new;
my $hal = slurp '../tmpl/hal.tmpl';
my $func_select = slurp '../tmpl/gsl_sf_functions.tmpl';
my $eval_div = slurp '../tmpl/eval.tmpl';

my $title = qq{<a href="$base_url/=">GSL REST API</a>};
my $MAX_POINTS = 500;
###
sub as_json($$) {
###
sub as_json($$) {
my ($q,$data) = @_;
return $q->header('application/json') . to_json($data, { pretty => 1 } );
}


sub cleanup_spew($) {
my $q = shift;
if (ref $@ and reftype $@ eq 'HASH') {
Expand All @@ -54,8 +60,8 @@ sub spew($$;$) {
my ($status, $title, $message) = @_;

die {
status => $status,
title => $title,
status => $status,
title => $title,
message => $message,
};
}
Expand All @@ -77,19 +83,31 @@ sub get_value($$) {
my ($f,$x) = @_;
my $y;
# close your eyes
my @mode_funcs = qw/ airy_Ai airy_Bi ellint_Kcomp ellint_Ecomp
ellint_Dcomp /;
my @mode_funcs = qw/ airy_Ai airy_Ai_scaled airy_Bi airy_Bi_scaled ellint_Kcomp ellint_Ecomp ellint_Dcomp airy_Ai_deriv airy_Bi_deriv airy_Ai_deriv_scaled airy_Bi_deriv_scaled /;
if ($f ~~ @mode_funcs ) {
$x = "$x,0"; # take care of dumb "mode" argument
}

$y = eval qq{ gsl_sf_$f($x) };
my @error_funcs = qw/ exp erf_Q erf_Z erf/;
my $result;
if ($f ~~ @error_funcs ) {
my $status;
$result = Math::GSL::SF::gsl_sf_result_struct->new;
$x = "$x,\$result";
warn "evaling \$status = gsl_sf_${f}_e($x) ";
eval qq{ \$status = gsl_sf_${f}_e($x) };
warn "status=$status";
die $@ if $@;
$y = $result->{val};
} else {
warn "evaling gsl_sf_$f($x) ";
$y = eval qq{ gsl_sf_$f($x) };
}

if ( $@ ) {
warn $@;
cleanup_spew($q);
}
if ($y =~ /nan/) {
if ($y =~ /nan/i) {
$y = 'NaN';
}
return $y;
Expand All @@ -102,16 +120,14 @@ eval {
$q->pre('GSL Web API pre-alpha');
};
GET qr{^/=/sf/$} => sub {
print $q->header('text/html') . $q->h1($title) .
print $q->header('text/html') . $q->h1($title) .
$q->h2('Special Function Documentation') .
$q->p('Example:') .
$q->p('Example:') .
$q->dl(
$q->dt(qq{ <a href="$base_url/=/sf/bessel_J0/2.582">$base_url/=/sf/<font color="red">bessel_J0</font>/2.582</a> } ),
$q->dd('Returns the Bessel Function J0 applied at x=2.582 as JSON'),
) .
$q->h2('Allowed Functions') .
$func_select .
qq{ <b>( <input type="text" id="x" size="4"> ) </b> <input type="submit" value="="> <input type="text" id="y" size="8"> };
$q->h2('Allowed Functions') . $eval_div ;
};
my $num_regex = qr/$RE{num}{real}{-base => 10}{-keep}/;
my $num_nokeep = qr/$RE{num}{real}{-base => 10}/;
Expand All @@ -138,18 +154,18 @@ eval {
if ( is_valid($function) ) {
my @linspace = map { $start + $_/$points } (1 .. $points);
my @values = map { get_value($function, $_) } @linspace;
print as_json($q, {
print as_json($q, {
'values' => \@values,
"start" => $start,
"end" => $end,
"points" => $points,
"points" => $points,
});
} else {
spew 503, 'Invalid Function', $hal;
}

};
};
cleanup_spew($q) if $@;
cleanup_spew($q) if $@;

print $q->header(-status => 404, -type => 'text/html') . $hal;
5 changes: 5 additions & 0 deletions css/layout.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
font-family: sans-serif;
font-size: 16px;
margin: 50px;
}
158 changes: 8 additions & 150 deletions eval.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="/plot/js/leto.js"></script>
<script src="/js/leto.js"></script>
<script src="/js/jquery.js"></script>
<script>

$(document).ready(function(){

$("#functionSubmit").click(function()
{
var url = "http://leto.net/rest/gsl.cgi/=/sf/" + $("select#function").val() + "/" + $("input#x").val();
var url = "http://localhost/~leto/hypatia/cgi/gsl.cgi/=/sf/" + $("select#function").val() + "/" + $("input#x").val();

debug('getting ' + url );

$("input#y").val("");

$.ajax({
dataType: "json",
url: url,
url: url,
error: function (XMLHttpRequest, textStatus, errorThrown) {
if ( textStatus ) {
debug( 'textStatus=' + textStatus );
} else {
debug( 'errorThrown=' + errorThrown );
} else {
debug( 'errorThrown=' + errorThrown );
}
this; // the options for this ajax request
},
Expand All @@ -36,155 +35,14 @@
return false;
})
});

</script>

</head>
<body>
<div id="eval">
<b>y(x)</b>=
<select id="function">
<option value="Chi">Chi</option>
<option value="Ci">Ci</option>
<option value="Shi">Shi</option>
<option value="Si">Si</option>
<option value="airy_Ai">airy_Ai</option>
<option value="airy_Ai_deriv">airy_Ai_deriv</option>
<option value="airy_Ai_deriv_scaled">airy_Ai_deriv_scaled</option>
<option value="airy_Ai_scaled">airy_Ai_scaled</option>
<option value="airy_Bi">airy_Bi</option>
<option value="airy_Bi_deriv">airy_Bi_deriv</option>
<option value="airy_Bi_deriv_scaled">airy_Bi_deriv_scaled</option>
<option value="airy_Bi_scaled">airy_Bi_scaled</option>
<option value="airy_zero_Ai">airy_zero_Ai</option>
<option value="airy_zero_Ai_deriv">airy_zero_Ai_deriv</option>
<option value="airy_zero_Bi">airy_zero_Bi</option>
<option value="airy_zero_Bi_deriv">airy_zero_Bi_deriv</option>
<option value="angle_restrict_pos">angle_restrict_pos</option>
<option value="angle_restrict_symm">angle_restrict_symm</option>
<option value="atanint">atanint</option>
<option value="bessel_I0">bessel_I0</option>
<option value="bessel_I0_scaled">bessel_I0_scaled</option>
<option value="bessel_I1">bessel_I1</option>
<option value="bessel_I1_scaled">bessel_I1_scaled</option>
<option value="bessel_In">bessel_In</option>
<option value="bessel_In_scaled">bessel_In_scaled</option>
<option value="bessel_Inu">bessel_Inu</option>
<option value="bessel_Inu_scaled">bessel_Inu_scaled</option>
<option selected="selected" value="bessel_J0">bessel_J0</option>
<option value="bessel_J1">bessel_J1</option>
<option value="bessel_Jn">bessel_Jn</option>
<option value="bessel_Jnu">bessel_Jnu</option>
<option value="bessel_K0">bessel_K0</option>
<option value="bessel_K0_scaled">bessel_K0_scaled</option>
<option value="bessel_K1">bessel_K1</option>
<option value="bessel_K1_scaled">bessel_K1_scaled</option>
<option value="bessel_Kn">bessel_Kn</option>
<option value="bessel_Kn_scaled">bessel_Kn_scaled</option>
<option value="bessel_Knu">bessel_Knu</option>
<option value="bessel_Knu_scaled">bessel_Knu_scaled</option>
<option value="bessel_Y0">bessel_Y0</option>
<option value="bessel_Y1">bessel_Y1</option>
<option value="bessel_Yn">bessel_Yn</option>
<option value="bessel_Ynu">bessel_Ynu</option>
<option value="bessel_i0_scaled">bessel_i0_scaled</option>
<option value="bessel_i1_scaled">bessel_i1_scaled</option>
<option value="bessel_i2_scaled">bessel_i2_scaled</option>
<option value="bessel_il_scaled">bessel_il_scaled</option>
<option value="bessel_j0">bessel_j0</option>
<option value="bessel_j1">bessel_j1</option>
<option value="bessel_j2">bessel_j2</option>
<option value="bessel_jl">bessel_jl</option>
<option value="bessel_k0_scaled">bessel_k0_scaled</option>
<option value="bessel_k1_scaled">bessel_k1_scaled</option>
<option value="bessel_k2_scaled">bessel_k2_scaled</option>
<option value="bessel_kl_scaled">bessel_kl_scaled</option>
<option value="bessel_lnKnu">bessel_lnKnu</option>
<option value="bessel_y0">bessel_y0</option>
<option value="bessel_y1">bessel_y1</option>
<option value="bessel_y2">bessel_y2</option>
<option value="bessel_yl">bessel_yl</option>
<option value="bessel_zero_J0">bessel_zero_J0</option>
<option value="bessel_zero_J1">bessel_zero_J1</option>
<option value="bessel_zero_Jnu">bessel_zero_Jnu</option>
<option value="clausen">clausen</option>
<option value="conicalP_0">conicalP_0</option>
<option value="conicalP_1">conicalP_1</option>
<option value="conicalP_cyl_reg">conicalP_cyl_reg</option>
<option value="conicalP_half">conicalP_half</option>
<option value="conicalP_mhalf">conicalP_mhalf</option>
<option value="conicalP_sph_reg">conicalP_sph_reg</option>
<option value="cos">cos</option>
<option value="dawson">dawson</option>
<option value="debye_1">debye_1</option>
<option value="debye_2">debye_2</option>
<option value="debye_3">debye_3</option>
<option value="debye_4">debye_4</option>
<option value="debye_5">debye_5</option>
<option value="debye_6">debye_6</option>
<option value="dilog">dilog</option>
<option value="gamma">gamma</option>
<option value="gamma_inc">gamma_inc</option>
<option value="gamma_inc_P">gamma_inc_P</option>
<option value="gamma_inc_Q">gamma_inc_Q</option>
<option value="gammainv">gammainv</option>
<option value="gammastar">gammastar</option>
<option value="gegenpoly_1">gegenpoly_1</option>
<option value="gegenpoly_2">gegenpoly_2</option>
<option value="gegenpoly_3">gegenpoly_3</option>
<option value="gegenpoly_n">gegenpoly_n</option>
<option value="hazard">hazard</option>
<option value="hydrogenicR">hydrogenicR</option>
<option value="hydrogenicR_1">hydrogenicR_1</option>
<option value="hypot">hypot</option>
<option value="hzeta">hzeta</option>
<option value="lambert_W0">lambert_W0</option>
<option value="lambert_Wm1">lambert_Wm1</option>
<option value="legendre_H3d">legendre_H3d</option>
<option value="legendre_H3d_0">legendre_H3d_0</option>
<option value="legendre_H3d_1">legendre_H3d_1</option>
<option value="legendre_P1">legendre_P1</option>
<option value="legendre_P2">legendre_P2</option>
<option value="legendre_P3">legendre_P3</option>
<option value="legendre_Pl">legendre_Pl</option>
<option value="legendre_Plm">legendre_Plm</option>
<option value="legendre_Q0">legendre_Q0</option>
<option value="legendre_Q1">legendre_Q1</option>
<option value="legendre_Ql">legendre_Ql</option>
<option value="legendre_sphPlm">legendre_sphPlm</option>
<option value="lncosh">lncosh</option>
<option value="lngamma">lngamma</option>
<option value="lnpoch">lnpoch</option>
<option value="lnsinh">lnsinh</option>
<option value="log">log</option>
<option value="log_1plusx">log_1plusx</option>
<option value="log_1plusx_mx">log_1plusx_mx</option>
<option value="log_abs">log_abs</option>
<option value="poch">poch</option>
<option value="pochrel">pochrel</option>
<option value="psi">psi</option>
<option value="psi_1">psi_1</option>
<option value="psi_1_int">psi_1_int</option>
<option value="psi_1piy">psi_1piy</option>
<option value="psi_int">psi_int</option>
<option value="psi_n">psi_n</option>
<option value="rect_to_polar">rect_to_polar</option>
<option value="sin">sin</option>
<option value="sinc">sinc</option>
<option value="synchrotron_1">synchrotron_1</option>
<option value="synchrotron_2">synchrotron_2</option>
<option value="taylorcoeff">taylorcoeff</option>
<option value="transport_2">transport_2</option>
<option value="transport_3">transport_3</option>
<option value="transport_4">transport_4</option>
<option value="transport_5">transport_5</option>
<option value="zeta">zeta</option>
<option value="zeta_int">zeta_int</option>
<option value="zetam1">zetam1</option>
<option value="zetam1_int">zetam1_int</option>
</select> <b>( <input type="text" id="x" size="4"> ) </b>
<b>y(x)</b>=<TMPL_VAR NAME="function_select">
<b>( <input type="text" id="x" size="4"> ) </b>
<input type="submit" id="functionSubmit" value="="> <input type="text" id="y" size="16">

</div>

</body>
Expand Down
Binary file added img/calculator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hypatia_fayum.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hypatia_fayum.xcf
Binary file not shown.
Binary file added img/hypatia_fayum2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hypatia_rounded.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions index.cgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/perl -w
# Read-only RESTful API to GSL Special Functions
# Jonathan "Duke" Leto <jonathan@leto.net> - Nov 2008
use strict;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use HTML::Template;

my $q = CGI->new;
my $tmpl_dir = 'tmpl/';
my $index_tmpl = HTML::Template->new(filename => "$tmpl_dir/index.tmpl");

# fill in some parameters
$index_tmpl->param( FOO => 42 );

print $q->header(-status => 200, -type => 'text/html');
print $index_tmpl->output;
2 changes: 1 addition & 1 deletion js/flotplot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $(function () {
xaxis: { ticks: 4 },
yaxis: { ticks: 4, base: 0 },
selection: { mode: "xy" },
grid: { hoverable: true, clickable: true , color: "#999"},
grid: { hoverable: true, clickable: true , color: "#999"}
};

var startData = getData( -2*Math.PI , 2*Math.PI );
Expand Down
4 changes: 2 additions & 2 deletions plot.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Plot your fancy</title>
<link href="layout.css" rel="stylesheet" type="text/css"></link>
<link href="css/layout.css" rel="stylesheet" type="text/css"></link>
<!--[if IE]><script language="javascript" type="text/javascript" src="/js/excanvas.pack.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="/js/jquery.js"> </script>
<script language="javascript" type="text/javascript" src="/js/jquery.flot-dev.js"> </script>
<script language="javascript" type="text/javascript" src="/js/jquery.flot.js"> </script>
</head>
<body>
<h1>Leto.net Plot</h1>
Expand Down
Loading

0 comments on commit 0d37c2a

Please sign in to comment.