]> git.lyx.org Git - wiki-uploads.git/blob - Modules/Sage/example.lyx
Import uploads from wiki
[wiki-uploads.git] / Modules / Sage / example.lyx
1 #LyX 2.0 created this file. For more info see http://www.lyx.org/
2 \lyxformat 413
3 \begin_document
4 \begin_header
5 \textclass article
6 \begin_preamble
7
8 \end_preamble
9 \use_default_options true
10 \begin_modules
11 sage
12 \end_modules
13 \maintain_unincluded_children false
14 \begin_local_layout
15 InsetLayout sagecommand
16 LyXType               custom
17 LabelString           Sage
18 LatexType             command
19 LatexName             sage
20 Decoration            classic
21 PassThru              1
22 Font
23   Color               magenta
24   Family              Typewriter
25 EndFont
26 MultiPar              false
27 OptionalArgs          0
28 End
29 \end_local_layout
30 \language american
31 \language_package default
32 \inputencoding utf8
33 \fontencoding global
34 \font_roman default
35 \font_sans default
36 \font_typewriter default
37 \font_default_family default
38 \use_non_tex_fonts false
39 \font_sc false
40 \font_osf false
41 \font_sf_scale 100
42 \font_tt_scale 100
43
44 \graphics default
45 \default_output_format default
46 \output_sync 0
47 \bibtex_command default
48 \index_command default
49 \paperfontsize default
50 \spacing single
51 \use_hyperref false
52 \papersize default
53 \use_geometry false
54 \use_amsmath 1
55 \use_esint 1
56 \use_mhchem 1
57 \use_mathdots 1
58 \cite_engine basic
59 \use_bibtopic false
60 \use_indices false
61 \paperorientation portrait
62 \suppress_date false
63 \use_refstyle 1
64 \index Index
65 \shortcut idx
66 \color #008000
67 \end_index
68 \secnumdepth 3
69 \tocdepth 3
70 \paragraph_separation indent
71 \paragraph_indentation default
72 \quotes_language english
73 \papercolumns 1
74 \papersides 1
75 \paperpagestyle default
76 \tracking_changes false
77 \output_changes false
78 \html_math_output 0
79 \html_css_as_file 0
80 \html_be_strict false
81 \end_header
82
83 \begin_body
84
85 \begin_layout Title
86 Example Integration of LyX with Sage
87 \end_layout
88
89 \begin_layout Author
90
91 \lang french
92 Original by: Murat Yildizoglu
93 \begin_inset Newline newline
94 \end_inset
95
96 Modified by: Thomas Coffee
97 \end_layout
98
99 \begin_layout Date
100
101 \lang french
102 March 30, 2012
103 \end_layout
104
105 \begin_layout Section
106 Overview
107 \end_layout
108
109 \begin_layout Standard
110 Sage (
111 \begin_inset CommandInset href
112 LatexCommand href
113 name "http://sagemath.org"
114 target "http://sagemath.org"
115
116 \end_inset
117
118 ) is an open source computer algebra system that integrates a wide variety
119  of open source mathematics software, with the mission of 
120 \begin_inset Quotes sld
121 \end_inset
122
123 creating a viable free open source alternative to Magma, Maple, Mathematica,
124  and Matlab.
125 \begin_inset Quotes srd
126 \end_inset
127
128  Sage uses Python as the glue for integrating nearly 100 open source packages
129  for mathematics, programming, and visualization.
130  Sage speaks LaTeX and can be used for literate programming that mixes normal
131  text with Sage instructions, directly including results in PS/PDF files.
132  The 
133 \family typewriter
134 sagetex.sty
135 \family default
136  package allows the input of Sage instructions in a LaTeX file (in a manner
137  similar to Sweave and R-Project).
138 \end_layout
139
140 \begin_layout Standard
141 The file sage.module provides the following Sage
142 \begin_inset ERT
143 status collapsed
144
145 \begin_layout Plain Layout
146
147 {}
148 \end_layout
149
150 \end_inset
151
152 TeX elements in LyX:
153 \end_layout
154
155 \begin_layout Itemize
156
157 \family typewriter
158 sageblock
159 \family default
160  and 
161 \family typewriter
162 sagesilent
163 \family default
164  environments for blocks of code made visible or invisible in the final
165  document, respectively
166 \end_layout
167
168 \begin_layout Itemize
169
170 \family typewriter
171 sageplot
172 \family default
173  and 
174 \family typewriter
175 sageplotcenter
176 \family default
177
178 \begin_inset Foot
179 status collapsed
180
181 \begin_layout Plain Layout
182 a customized call to 
183 \family typewriter
184 sageplot
185 \family default
186  defined in the sage.module file; you can define similar environments with
187  different options to conveniently generate multiple plots of different
188  styles
189 \end_layout
190
191 \end_inset
192
193  environments that can be used for plotting code inside a Figure float
194 \end_layout
195
196 \begin_layout Itemize
197
198 \family typewriter
199 sagecommand
200 \family default
201  and 
202 \family typewriter
203 sageinline
204 \family default
205  insets
206 \begin_inset Foot
207 status collapsed
208
209 \begin_layout Plain Layout
210 accessible via the menu item Insert >> Custom Insets
211 \end_layout
212
213 \end_inset
214
215  that can be used inline, where results of the latter appear in math mode
216 \end_layout
217
218 \begin_layout Itemize
219
220 \family typewriter
221 sagedisplay
222 \family default
223  environment, whose results appear in display math mode
224 \begin_inset Foot
225 status collapsed
226
227 \begin_layout Plain Layout
228 you can define similar environments in sage.module to support other math
229  environments
230 \end_layout
231
232 \end_inset
233
234
235 \end_layout
236
237 \begin_layout Standard
238 The Sage
239 \begin_inset ERT
240 status collapsed
241
242 \begin_layout Plain Layout
243
244 {}
245 \end_layout
246
247 \end_inset
248
249 TeX commands 
250 \family typewriter
251
252 \backslash
253 sage
254 \family default
255  and 
256 \family typewriter
257
258 \backslash
259 sageplot
260 \family default
261 , along with other custom commands defined in sage.module, can also be used
262  as ERT.
263  This can be useful in math mode when results from Sage should be combined
264  seamlessly with other equation components.
265  Note that LyX's special handling of math mode formatting can cause problems
266  for some Sage input: for this reason, it may be necessary to assign a result
267  to a named variable using a 
268 \family typewriter
269 sagesilent
270 \family default
271  environment and include the variable by name in ERT where the result should
272  appear.
273 \end_layout
274
275 \begin_layout Standard
276 When a Sage
277 \begin_inset ERT
278 status collapsed
279
280 \begin_layout Plain Layout
281
282 {}
283 \end_layout
284
285 \end_inset
286
287 TeX document 
288 \family typewriter
289 mydocument.tex
290 \family default
291  is compiled with (pdf)latex, it automatically generates a Sage file 
292 \family typewriter
293 mydocument.sage
294 \family default
295  that can be run through Sage using the command 
296 \family typewriter
297 sage mydocument.sage
298 \family default
299 .
300  This compiles the results of the Sage instructions and a supplementary
301  run through (pdf)latex includes these results (computations, plots, etc.)
302  in the final document.
303  The files included with this example provide a basic capability to combine
304  this functionality with LyX, which you can extend to meet your specific
305  needs.
306 \end_layout
307
308 \begin_layout Standard
309 Using this module, to compile a document with embedded Sage code one must
310  view the output as 
311 \begin_inset Quotes eld
312 \end_inset
313
314 PDF (pdflatex+sagetex)
315 \begin_inset Quotes erd
316 \end_inset
317
318 , located by default under 
319 \begin_inset Quotes eld
320 \end_inset
321
322 Other Formats.
323 \begin_inset Quotes erd
324 \end_inset
325
326  To update only the LaTeX without updating any results from Sage, one can
327  use the default output format 
328 \begin_inset Quotes eld
329 \end_inset
330
331 PDF (pdflatex)
332 \begin_inset Quotes erd
333 \end_inset
334
335 .
336  This default can be changed in the included 
337 \family typewriter
338 preferences
339 \family default
340  file.
341 \end_layout
342
343 \begin_layout Standard
344 If you are working under GNU/Linux, the included shell script 
345 \family typewriter
346 setup.sh
347 \family default
348  can be customized to place the necessary components where they belong.
349  This assumes you have already installed and tested LyX and Sage independently,
350  and includes the necessary commands to include Sage
351 \begin_inset ERT
352 status collapsed
353
354 \begin_layout Plain Layout
355
356 {}
357 \end_layout
358
359 \end_inset
360
361 TeX in a common LaTeX installation.
362 \begin_inset Foot
363 status collapsed
364
365 \begin_layout Plain Layout
366 based on 
367 \begin_inset CommandInset href
368 LatexCommand href
369 name "http://www.sagemath.org/doc/installation/sagetex.html"
370 target "http://www.sagemath.org/doc/installation/sagetex.html"
371
372 \end_inset
373
374
375 \end_layout
376
377 \end_inset
378
379  After establishing the configuration, you will need to run Tools >> Reconfigure
380  from the LyX menu.
381  This configuration has been successfully tested using the following setup:
382 \end_layout
383
384 \begin_layout Itemize
385 Ubuntu 11.10 x86_64
386 \end_layout
387
388 \begin_layout Itemize
389 LyX 2.0.0 with TeXLive 2009-13 (but should work with LyX 1.6+)
390 \end_layout
391
392 \begin_layout Itemize
393 Sage 4.8 [2012-01-20]
394 \end_layout
395
396 \begin_layout Standard
397 Note that in earlier versions of Sage, Sage
398 \begin_inset ERT
399 status collapsed
400
401 \begin_layout Plain Layout
402
403 {}
404 \end_layout
405
406 \end_inset
407
408 TeX may produce files with extension 
409 \family typewriter
410 .sage
411 \family default
412  rather than 
413 \family typewriter
414 .sagetex.sage
415 \family default
416 .
417  You may need to modify the 
418 \family typewriter
419 preferences
420 \family default
421  and 
422 \family typewriter
423 compile-pdf-sage.sh
424 \family default
425  files accordingly.
426 \end_layout
427
428 \begin_layout Section
429 Examples
430 \end_layout
431
432 \begin_layout Standard
433 Sage can output LaTeX: 
434 \begin_inset Flex sageinline
435 status open
436
437 \begin_layout Plain Layout
438
439 matrix([[1, 2], [3,4]])^2
440 \end_layout
441
442 \end_inset
443
444
445 \end_layout
446
447 \begin_layout Standard
448
449 \lang french
450 Sage can do plots: Figure 
451 \begin_inset CommandInset ref
452 LatexCommand ref
453 reference "fig:Test-de-figure"
454
455 \end_inset
456
457 .
458 \end_layout
459
460 \begin_layout Standard
461
462 \lang french
463 \begin_inset Float figure
464 wide false
465 sideways false
466 status open
467
468 \begin_layout sageplotcenter
469
470 plot(sin(x), 0, pi), axes=True
471 \end_layout
472
473 \begin_layout Plain Layout
474
475 \lang french
476 \begin_inset Caption
477
478 \begin_layout Plain Layout
479
480 \lang french
481 The sine function
482 \begin_inset CommandInset label
483 LatexCommand label
484 name "fig:Test-de-figure"
485
486 \end_inset
487
488
489 \end_layout
490
491 \end_inset
492
493
494 \end_layout
495
496 \end_inset
497
498
499 \end_layout
500
501 \begin_layout Standard
502
503 \lang english
504 Sage
505 \begin_inset ERT
506 status collapsed
507
508 \begin_layout Plain Layout
509
510 {}
511 \end_layout
512
513 \end_inset
514
515 TeX can include blocks for defining and computing elements.
516  In this case, the block is not typeset in the final document, since we
517  use the 
518 \family typewriter
519 sagesilent
520 \family default
521  environment.
522  We declare 
523 \begin_inset Formula $x$
524 \end_inset
525
526  as a variable and 
527 \begin_inset Formula $y=x^{2}+5x+2$
528 \end_inset
529
530 .
531 \end_layout
532
533 \begin_layout sagesilent
534
535 x=var('x')
536 \begin_inset Newline newline
537 \end_inset
538
539 y=x^2+5*x+2
540 \end_layout
541
542 \begin_layout Standard
543 We can now compute an equation with an unknown 
544 \begin_inset Formula $x$
545 \end_inset
546
547 : the roots of equation 
548 \begin_inset Formula $x^{2}+5x+2$
549 \end_inset
550
551  are hence given by (computed by Sage):
552 \end_layout
553
554 \begin_layout sagedisplay
555
556 solve(y,x)
557 \end_layout
558
559 \begin_layout Standard
560 Another equation, third degree of 
561 \begin_inset Formula $x$
562 \end_inset
563
564  this time: roots of 
565 \begin_inset Formula $x^{3}-10$
566 \end_inset
567
568  are: 
569 \end_layout
570
571 \begin_layout sagedisplay
572
573 solve(x^3-10,x)
574 \end_layout
575
576 \begin_layout Standard
577 A system of two equations:
578 \end_layout
579
580 \begin_layout sageblock
581
582 x,y=var('x y')
583 \end_layout
584
585 \begin_layout sageblock
586
587 z=solve([x+y==6,x-y==4],x,y)
588 \end_layout
589
590 \begin_layout Standard
591 The roots of the system
592 \lang french
593
594 \begin_inset Formula 
595 \[
596 \begin{array}{ccc}
597 x+y & = & 6\\
598 x-y & = & 4
599 \end{array}
600 \]
601
602 \end_inset
603
604 are given by:
605 \end_layout
606
607 \begin_layout sagedisplay
608
609 z
610 \end_layout
611
612 \begin_layout Standard
613 Sage can do differentiation:
614 \end_layout
615
616 \begin_layout sageblock
617
618 y=sin(x^2)
619 \end_layout
620
621 \begin_layout Standard
622 \align center
623
624 \lang french
625 \begin_inset Formula $\dfrac{\partial^{4}y}{\partial x^{4}}=\sage{diff(y,x,4)}$
626 \end_inset
627
628
629 \end_layout
630
631 \begin_layout Standard
632 Sage can also do integrals:
633 \lang french
634  
635 \begin_inset Formula 
636 \[
637 \int_{0}^{1}\frac{x}{x^{2}+1}
638 \]
639
640 \end_inset
641
642
643 \lang american
644 is given by 
645 \begin_inset Flex sageinline
646 status open
647
648 \begin_layout Plain Layout
649
650 integral(x/(x^2+1),x,0,1)
651 \end_layout
652
653 \end_inset
654
655 .
656 \end_layout
657
658 \begin_layout Standard
659 Sage can solve differential equations: we declare a variable 
660 \begin_inset Formula $t$
661 \end_inset
662
663 , a function of 
664 \begin_inset Formula $t$
665 \end_inset
666
667
668 \begin_inset Formula $x(t)$
669 \end_inset
670
671  and a differential equation
672 \lang french
673  
674 \begin_inset Formula 
675 \[
676 \frac{dx}{dt}+x-1=0
677 \]
678
679 \end_inset
680
681
682 \end_layout
683
684 \begin_layout sageblock
685
686 t = var('t')
687 \end_layout
688
689 \begin_layout sageblock
690
691 x = function('x',t)
692 \end_layout
693
694 \begin_layout sageblock
695
696 DE = diff(x, t) + x - 1
697 \end_layout
698
699 \begin_layout Standard
700 We can now solve the equation: 
701 \end_layout
702
703 \begin_layout Standard
704 \align center
705 \begin_inset Formula $x(t)=\sage{desolve(DE,[x,t])}$
706 \end_inset
707
708
709 \end_layout
710
711 \end_body
712 \end_document