Skip to content

Instantly share code, notes, and snippets.

View jasongrout's full-sized avatar

Jason Grout jasongrout

View GitHub Profile
import contextlib
@contextlib.contextmanager
def timing(results=None):
"""
Time the execution of the block of code. If a results list is
passed in, the time is appended to the list. Also returns a list
of one element containing the time the execution took.
To use, do something like:
class mydecorator(object):
def __init__(self, times, repeats):
self.times=times
self.repeats=repeats
self.a=1
def __call__(self, func):
# here, we just need to return a new function. That new function will be what is actually stored in the name and run when I.
def myfunc(*args, **kwargs):
# *this* inner function is what is actually called
@jasongrout
jasongrout / gist:1010165
Created June 6, 2011 12:33
Efficient sum that doesn't try to simplify in Sage
def fsum(x,a):
g(x) = 1/2
list_of_terms=[2*(((-1)^n-1)/(n*pi)^2*cos(n*pi*x)-(-1)^n/(n*pi)*sin(n*pi*x)) for n in [1..a]]
g += sum(list_of_terms)
return g
@jasongrout
jasongrout / myfile.sage
Created August 1, 2011 23:40
test sage code
print 'hi'
@jasongrout
jasongrout / embed_sage.html
Created August 2, 2011 16:08
Embed a single cell server in a web page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>Simple Compute Server</title>
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
<script>
TEXT(<<'EOF');
<div id="singlecell-test"><script type="text/code">var('x')
x0 = 0
f(x) = sin(x)*e^(-x)
p = plot(f,(x,-1,5), thickness=2)
dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0))
@interact
def _(order=(1..12)):
ft = f.taylor(x,x0,order)
pt = plot(ft,(x,-1, 5), color='green', thickness=2)
@jasongrout
jasongrout / gist:1675277
Created January 25, 2012 07:54
Make python mode and rst mode have tab/shift-tab indent/dedent selected lines
;; Enable tab and shift-tab to continuously indent/dedent rst blocks
(add-hook 'rst-mode-hook
(function (lambda ()
(setq indent-region-function (lambda (&rest n)
(rst-shift-region-right nil)
(setq deactivate-mark nil)))
(local-set-key [S-tab] (lambda (&rest n)
(interactive)
(if (use-region-p)
(progn (rst-shift-region-left nil)
@jasongrout
jasongrout / gist:2221289
Created March 27, 2012 23:03
Webwork problem
## DESCRIPTION
## Linear Algebra
## ENDDESCRIPTION
## KEYWORDS('matrix' 'null space' 'vector')
## Tagged by tda2d
## DBsubject('Linear Algebra')
## DBchapter('Matrices')
## DBsection('Image and Kernel')
# -*- coding: iso-8859-1 -*-
"""
MoinMoin - Sage Cell Parser
@copyright: 2010 by Chris Martino <chris@console.org>, Jason Grout <jason-sage@creativetrax.com>
@license: GNU GPL.
"""
from MoinMoin.parser._ParserBase import ParserBase
from uuid import uuid4
{
"content": [
{
"parent_header": {
"username": "",
"msg_id": "4f5e129c-da63-4db2-9bf6-d2ad89b898fa",
"session": "c9ec7d91-6a89-4aa4-bdf5-f1d55bbd387c"
},
"msg_type": "extension",
"sequence": 0,