The Sage Notebook
The Notebook interfaces with your software
Here is an example of using GAP from the notebook.
{{{id=3| %gap a := AlternatingGroup(8) /// }}} {{{id=9| %gap Size(a) /// }}}GAP commands are also available as gap.<gapcommand> which returns a Python object.
{{{id=12| S8 = gap.Group('(1,2)','(1,2,3,4,5,6,7,8)') S8 /// }}} {{{id=13| type(S8) /// }}} {{{id=14| S8.Size() /// }}} {{{id=16| G = gap.SylowSubgroup(S8, 2); G /// }}} {{{id=15| G.IsNormal(S8) /// }}} {{{id=17| /// }}}Type p. and then hit TAB.
{{{id=21| p. /// }}} {{{id=28| print p.ferrers_diagram() /// }}}To view the documentation and examples for the core method, type p.core? and hit TAB.
{{{id=27| p.core? /// }}}To view the source code for the core method, type p.core?? and hit TAB.
{{{id=30| p.core?? /// }}}Yoda!
50,000 triangles!
{{{id=34| from scipy import io x = io.loadmat(DATA + 'yodapose.mat') from sage.plot.plot3d.index_face_set import IndexFaceSet V = x['V']; F3 = x['F3']-1; F4 = x['F4']-1 Y = (IndexFaceSet(F3, V, color = Color('#00aa00')) + IndexFaceSet(F4, V, color = Color('#00aa00'))) Y = Y.rotateX(-1) Y.show(aspect_ratio = [1,1,1], frame = False, figsize = 4) /// }}} {{{id=33| /// }}} {{{id=32| /// }}}
@interact with your mathematics!
{{{id=40| def plottaylor(order=(1..15)): var('x') f = sin(x) * e^(-x) g = f.taylor(x, 0, order) F = plot(f,-1, 5, thickness=2) G = plot(g,-1, 5, color='green', thickness=2) show(F+G, ymin = -.5, ymax = 1) /// }}} {{{id=39| plottaylor(1) /// }}} {{{id=38| @interact def plottaylor(order=(1..15)): var('x') f = sin(x) * e^(-x) g = f.taylor(x, 0, order) html('$f(x)\;=\;%s$'%latex(f)) html('$\hat{f}(x;%s)\;=\;%s+\mathcal{O}(x^{%s})$'%(0,latex(g),order+1)) F = plot(f,-1, 5, thickness=2) G = plot(g,-1, 5, color='green', thickness=2) show(F+G, ymin = -.5, ymax = 1) /// }}}
@interact with your mathematics!
{{{id=37| %hide import urllib class Day: def __init__(self, date, open, high, low, close, volume): self.date = date self.open=float(open); self.high=float(high); self.low=float(low); self.close=float(close) self.volume=int(volume) def __repr__(self): return '%10s %4.2f %4.2f %4.2f %4.2f %10d'%(self.date, self.open, self.high, self.low, self.close, self.volume) class Stock: def __init__(self, symbol): self.symbol = symbol.upper() def __repr__(self): return "%s (%s)"%(self.symbol, self.yahoo()['price']) def yahoo(self): url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (self.symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7') values = urllib.urlopen(url).read().strip().strip('"').split(',') data = {} data['price'] = values[0] data['change'] = values[1] data['volume'] = values[2] data['avg_daily_volume'] = values[3] data['stock_exchange'] = values[4] data['market_cap'] = values[5] data['book_value'] = values[6] data['ebitda'] = values[7] data['dividend_per_share'] = values[8] data['dividend_yield'] = values[9] data['earnings_per_share'] = values[10] data['52_week_high'] = values[11] data['52_week_low'] = values[12] data['50day_moving_avg'] = values[13] data['200day_moving_avg'] = values[14] data['price_earnings_ratio'] = values[15] data['price_earnings_growth_ratio'] = values[16] data['price_sales_ratio'] = values[17] data['price_book_ratio'] = values[18] data['short_ratio'] = values[19] return data def historical(self): try: return self.__historical except AttributeError: pass symbol = self.symbol def get_data(exchange): name = get_remote_file('http://finance.google.com/finance/historical?q=%s:%s&output=csv'%(exchange, symbol.upper()), verbose=False) return open(name).read() R = get_data('NASDAQ') if "Bad Request" in R: R = get_data("NYSE") R = R.splitlines() headings = R[0].split(',') self.__historical = [] try: for x in reversed(R[1:]): date, opn, high, low, close, volume = x.split(',') self.__historical.append(Day(date, opn,high,low,close,volume)) except ValueError: pass self.__historical = Sequence(self.__historical,cr=True,universe=lambda x:x) return self.__historical def plot_average(self, spline_samples=10): d = self.historical() if len(d) == 0: return text('no historical data at Google Finance about %s'%self.symbol, (0,3)) avg = list(enumerate([(z.high+z.low)/2 for z in d])) P = line(avg) + points(avg, rgbcolor='black', pointsize=4) + \ text(self.symbol, (len(d)*1.05, d[-1].low), horizontal_alignment='right', rgbcolor='black') if spline_samples > 0: k = 250//spline_samples spl = spline([avg[i*k] for i in range(len(d)//k)] + [avg[-1]]) P += plot(spl, (0,len(d)+30), color=(0.7,0.7,0.7)) P.xmax(260) return P def plot_diff(self): d = self.historical() if len(d) == 0: return text('no historical data at Google Finance about %s'%self.symbol, (0,3)) diff = [] for i in range(1, len(d)): z1 = d[i]; z0 = d[i-1] diff.append((i, (z1.high+z1.low)/2 - (z0.high + z0.low)/2)) P = line(diff,thickness=0.5) + points(diff, rgbcolor='black', pointsize=4) + \ text(self.symbol, (len(d)*1.05, 0), horizontal_alignment='right', rgbcolor='black') P.xmax(260) return P symbols = ['bsc', 'vmw', 'sbux', 'aapl', 'amzn', 'goog', 'wfmi', 'msft', 'yhoo', 'ebay', 'java', 'rht', ]; symbols.sort() stocks = dict([(s,Stock(s)) for s in symbols]) @interact def data(symbol = symbols, other_symbol='', spline_samples=(8,[0..15])): if other_symbol != '': symbol = other_symbol S = Stock(symbol) html('
%s | %s |
{{{id=47| /// }}}
A Cython Example
Pure Python: sum the first $N$ positive integers: $\sum_{k=1}^N k$
{{{id=62| def sum_first_N_python(N): s = int(0) for k in range(1,N+1): s += k return s /// }}} {{{id=59| time sum_first_N_python(10^7) /// }}}TWO CHANGES: declare $k$ to be a C int; declare $s$ to be a C long long.
{{{id=31| /// }}}
Community
Asking Questions
{{{id=66| /// }}}
Netiquette
{{{id=65| /// }}}