Sage useful commands to make homework keys in Algebra & Calculus.
1. To save the definitions of all functions and variables including history of input and output commands:
%colors NoColor
save_session() or save_session(‘2014.12.07’)
The saved session can befound in the user home directory (~), for example, 2014.12.07.sobj.
To restore a particular session:
load_session (‘2014.12.07.sobj’)
_oh (to list all output lines in the session, Python formatted).
_ih (to list all input commands in the session, Python formatted).
The original command can be reconstructed without type declaration.
2. To see the definition of a function or a variable (for example, dist formula)
show_identifiers()
dist
3. Set up log file with correct parameters (Python’s output, raw input, with timestamp). The log file is saved in the current directory RealNumber(2014.12).gen(09).log.
logstart -r -o -t 2014.12.09.log append
logstop to stop the log file recording.
4. Short definitions.
\(d=\sqrt{(x_{1}-x_{2})^2+(y_{1}-y_{2})^2}\)
sage: s_dis(x1,y1,x2,y2)=sqrt((x1 – x2)^2 + (y1 – y2)^2)
sage: s_dis(1,3,2,5)
sage: def l_dis(x1,y1,x2,y2):
….: print “Distance from (%1.2f,%1.2f) to (%1.2f,%1.2f) = %1.2f”%(x1,y1,x2,y2,sqrt((x1 – x2)^2 + (y1 – y2)^2))
….: (Enter a blank line return to end)
sage: l_dis(2,0,3,2)
Distance from (2.00,0.00) to (3.00,2.00) = 2.24
6. To convert a linear equation from standard form (\(2y+6x\)\(-3=7-2x\)) to slope-intercept form by solving for y (\(y=-4x+5\)):
sage: solve(2*y+6*x-3==7-2*x,y)
[y == -4*x + 5]
7. Execute a C-interface program. It may be necessary to stop the logging process (logstop) to disable warning/error messages relating to the logging process. Remember to restart the log when done.
See example of distance formula in homemath.c and homemath.spyx.
Notes:
7.1. C-syntax for power notation is different from sage power syntax (^).
7.2. Updating homemath.spyx automatically updates the compilation process.
7.3. The C-interface program is really in Python indented format.
8. Execute a Linux command (php -f bc.php from a function) to support custom solutions (Roman symbols).
l_rom(200) (.spyx interface)
!php -f bc.php ‘rom(200)’ (command shell interface)
9. Calculus functions.
\(f(x)=2x^2+4x+3\)
\(f'(x)=4x+4\)
f(x)=2*x^2+4*x+3
f(x).derivative(x)
10. Statistical functions.
mean, median, mode, range, max, min, variance, std, sort
v=[2,6,7,9,13,15]
v
mean(v)
sum(v)
median(v)
u=[13,15,2,6,9,7]
mean(u)
u
max(u)
min(u)
range(u)
def range(u):max(u)-min(u)
range(u)
range
u
range(u)
def range(u): return (max(u)-min(u))
range(u)
range
mode(u)
u=[13,15,2,6,9,7,7,7]
mode(u)
std(u)
variance(u)
u.sort()
sorted(u)
sorted(u,reverse=True)
9. Print format options.
10. Plotting optiọns
10. Save individual objects to build a local math reference library.
11. Build a sage server for worksheet development (grade 3-12).