Merged heads.
--- a/lstsq.rst Thu Sep 23 11:59:35 2010 +0530
+++ b/lstsq.rst Thu Sep 23 12:15:27 2010 +0530
@@ -1,3 +1,8 @@
+.. Author : Nishanth
+ Internal Reviewer 1 : Puneeth
+ Internal Reviewer 2 :
+ External Reviewer :
+
Hello friends and welcome to the tutorial on Least Square Fit
{{{ Show the slide containing title }}}
@@ -17,31 +22,14 @@
pendulum and the second is the corresponding time period of the pendulum.
As we know, the square of time period of a pendulum is directly proportional to
-its length, we shall plot l vs t^2 and verify if the proportionality is linear.
-
-If it is not linear, we shall generate a least square fit line.
-
-{{{ show the slide containing explanation on least square fit }}}
-
-As shown in the slide, we are first going to generate the two matrices tsq and
-A. Then we are going to use the =lstsq= function to find the values of m and c.
-
-To read the input file and parse the data, we are going to loadtxt function.
-Type
-::
+its length, we shall plot l vs t^2 and verify this.
- data = loadtxt("/home/fossee/pendulum.txt")
- data
+#[Puneeth:] removed the explanation about loadtxt and unpack
+ option. It's been done in another LO already. simple dependency
+ should work?
-As you can see, data is a sequence containing 90 records. Each record contains
-two values. The first is length and second is time period. But what we need is
-two sequences. One sequence containing all the length values and one containing
-all the time values.
-
-Hence we have to use the unpack option with loadtxt. It unpacks the data into
- sequences depending on the structure of data.
-
-Type
+To read the input file and parse the data, we are going to use the
+loadtxt function. Type
::
l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
@@ -57,10 +45,20 @@
tsq = t * t
plot(l, tsq, 'bo')
-
{{{ switch to the plot window }}}
-We can see that there is a visible linear trend.
+#[Puneeth:] Moved explanation of least square fit here. seems more
+apt.
+
+We can see that there is a visible linear trend, but we do not get a
+straight line connecting them. We shall, therefore, generate a least
+square fit line.
+
+{{{ show the slide containing explanation on least square fit }}}
+
+As shown in the slide, we are first going to generate the two matrices
+tsq and A. Then we are going to use the ``lstsq`` function to find the
+values of m and c.
let us now generate the A matrix with l values.
We shall first generate a 2 x 90 matrix with the first row as l values and the
@@ -70,20 +68,20 @@
inter_mat = array((l, ones_like(l)))
inter_mat
-We see that we have intermediate matrix. Now we need the transpose.Type
+We see that we have intermediate matrix. Now we need the transpose. Type
::
A = inter_mat.T
A
-Now we have both the matrices A and tsq. We only need to use the =lstsq=
+Now we have both the matrices A and tsq. We only need to use the ``lstsq``
Type
::
result = lstsq(A, tsq)
-The result is a sequence of values. The first item is the matrix p or in simple
-words, the values of m and c. Hence,
+The result is a sequence of values. The first item in this sequence,
+is the matrix p i.e., the values of m and c. Hence,
::
m, c = result[0]
@@ -120,9 +118,5 @@
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
Hope you have enjoyed and found it useful.
-Thankyou
+Thank you
-.. Author : Nishanth
- Internal Reviewer 1 :
- Internal Reviewer 2 :
- External Reviewer :
--- a/parsing_data.rst Thu Sep 23 11:59:35 2010 +0530
+++ b/parsing_data.rst Thu Sep 23 12:15:27 2010 +0530
@@ -1,3 +1,8 @@
+.. Author : Nishanth
+ Internal Reviewer 1 :
+ Internal Reviewer 2 :
+ External Reviewer :
+
Hello friends and welcome to the tutorial on Parsing Data
{{{ Show the slide containing title }}}
@@ -6,15 +11,20 @@
In this tutorial, we shall learn
- * What is parsing data
+ * What we mean by parsing data
* the string operations required for parsing data
* datatype conversion
+#[Puneeth]: Changed a few things, here.
+
+#[Puneeth]: I don't like the way the term "parsing data" has been used, all
+through the script. See if that can be changed.
+
Lets us have a look at the problem
{{{ Show the slide containing problem statement. }}}
-There is an input file containing huge no.of records. Each record corresponds
+There is an input file containing huge no. of records. Each record corresponds
to a student.
{{{ show the slide explaining record structure }}}
@@ -28,16 +38,22 @@
#[Nishanth]: Please note that I am not telling anything about AA since they do
not know about any if/else yet.
+#[Puneeth]: Should we talk pass/fail etc? I think we should make the problem
+ simple and leave out all the columns after total marks.
-So what exactly is parsing data?
+Now what is parsing data.
+From the input file, we can see that the data we have is in the form of
+text. Parsing this data is all about reading it and converting it into a form
+which can be used for computations -- in our case, sequence of numbers.
-Parsing data is all about reading the data and converting it into a form which
-can be used for computations. In our case, that is numbers.
+#[Puneeth]: should the word tokenizing, be used? Should it be defined before
+ using it?
We can clearly see that the problem involves reading files and tokenizing.
-.. #[[Amit:Definition of Tokenizing here.]]
+#[Puneeth]: the sentence above seems kinda redundant.
+
Let us learn about tokenizing strings. Let us define a string first. Type
::
@@ -48,11 +64,11 @@
line.split()
-As you can see, we get a list of strings. Which means, when split is called
+As you can see, we get a list of strings. Which means, when ``split`` is called
without any arguments, it splits on whitespace. In simple words, all the spaces
are treated as one big space.
-split also can split on a string of our choice. This is acheived by passing
+``split`` also can split on a string of our choice. This is acheived by passing
that as an argument. But first lets define a sample record from the file.
::
@@ -63,8 +79,8 @@
We can also observe that an empty string appears in the list since there are
two semi colons without anything in between.
-Hence split splits on whitespace if called without an argument and splits on
-the given argument if it is called with an argument.
+To recap, ``split`` splits on whitespace if called without an argument and
+splits on the given argument if it is called with an argument.
{{{ Pause here and try out the following exercises }}}
@@ -76,13 +92,13 @@
We see that when we split on space, multiple whitespaces are not clubbed as one
and there is an empty string everytime there are two consecutive spaces.
-Now that we know how to split a string, we can split the record and retreive each
-field seperately. But there is one problem. The region code "B" and a "B"
+Now that we know how to split a string, we can split the record and retrieve
+each field seperately. But there is one problem. The region code "B" and a "B"
surrounded by whitespace are treated as two different regions. We must find a
way to remove all the whitespace around a string so that "B" and a "B" with
white spaces are dealt as same.
-This is possible by using the =strip= method of strings. Let us define a
+This is possible by using the ``strip`` method of strings. Let us define a
string by typing
::
@@ -110,20 +126,21 @@
white space. The only road block we now have is conversion of string to float.
The splitting and stripping operations are done on a string and their result is
-also a string, hence the marks that we have are still strings and mathematical
-operations on them are not possible. We must convert them into integers or floats
+also a string. hence the marks that we have are still strings and mathematical
+operations are not possible on them. We must convert them into numbers
+(integers or floats), before we can perform mathematical operations on them.
-We shall look at converting strings into floats. We define an float string
-first. Type
+We shall look at converting strings into floats. We define a float string
+first. Type
::
mark_str = "1.25"
- mark = float(mark_str)
+ mark = int(mark_str)
type(mark_str)
type(mark)
We can see that string is converted to float. We can perform mathematical
-operations on it now.
+operations on them now.
{{{ Pause here and try out the following exercises }}}
@@ -131,8 +148,6 @@
{{{ continue from paused state }}}
-.. #[[Amit:I think there should be some interaction first here about the
-problem before we conclude to talking about the result.]]
It raises an error since converting a float string into integer directly is
not possible. It involves an intermediate step of converting to float.
::
@@ -143,7 +158,7 @@
number = int(flt)
number
-Using =int= it is possible to convert float into integers.
+Using ``int`` it is also possible to convert float into integers.
Now that we have all the machinery required to parse the file, let us solve the
problem. We first read the file line by line and parse each record. We see if
@@ -162,7 +177,7 @@
if region_code == "AA":
math_marks_B.append(math_mark)
-.. #[[Amit:This intutively does not seem to be what you wanted]]
+
Now we have all the maths marks of region "B" in the list math_marks_B.
To get the mean, we just have to sum the marks and divide by the length.
@@ -179,7 +194,6 @@
* how to tokenize a string using various delimiters
* how to get rid of extra white space around
* how to convert from one type to another
-.. #[[Amit:one datatype to another may be better.]]
* how to parse input data and perform computations on it
{{{ Show the "sponsored by FOSSEE" slide }}}
@@ -188,9 +202,5 @@
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
Hope you have enjoyed and found it useful.
-Thankyou
+Thank you
-.. Author : Nishanth
- Internal Reviewer 1 : Amit Sethi
- Internal Reviewer 2 :
- External Reviewer :
--- a/using-sage.rst Thu Sep 23 11:59:35 2010 +0530
+++ b/using-sage.rst Thu Sep 23 12:15:27 2010 +0530
@@ -22,7 +22,8 @@
We have our Sage notebook running. In case, you don't have it running,
start is using the command, ``sage --notebook``.
-To find the limit of the function x*sin(1/x), at x=0, we say::
+To find the limit of the function x*sin(1/x), at x=0, we say
+::
lim(x*sin(1/x), x=0)
@@ -30,18 +31,21 @@
It is also possible to the limit at a point from one direction. For
example, let us find the limit of 1/x at x=0, when approaching from
-the positive side.::
+the positive side.
+::
lim(1/x, x=0, dir='above')
-To find the limit from the negative side, we say,::
+To find the limit from the negative side, we say,
+::
lim(1/x, x=0, dir='above')
Let us now see how to differentiate, using Sage. We shall find the
differential of the expression ``exp(sin(x^2))/x`` w.r.t ``x``. We
shall first define the expression, and then use the ``diff`` function
-to obtain the differential of the expression.::
+to obtain the differential of the expression.
+::
var('x')
f = exp(sin(x^2))/x
@@ -50,7 +54,8 @@
We can also obtain the partial differentiation of an expression w.r.t
one of the variables. Let us differentiate the expression
-``exp(sin(y - x^2))/x`` w.r.t x and y.::
+``exp(sin(y - x^2))/x`` w.r.t x and y.
+::
var('x y')
f = exp(sin(y - x^2))/x
@@ -62,7 +67,8 @@
Now, let us look at integration. We shall use the expression obtained
from the differentiation that we did before, ``diff(f, y)`` ---
``e^(sin(-x^2 + y))*cos(-x^2 + y)/x``. The ``integrate`` command is
-used to obtain the integral of an expression or function.::
+used to obtain the integral of an expression or function.
+::
integrate(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y)
@@ -70,13 +76,15 @@
outside the ``sin`` function doesn't change much.
Now, let us find the value of the integral between the limits 0 and
-pi/2. ::
+pi/2.
+::
integral(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y, 0, pi/2)
Let us now see how to obtain the Taylor expansion of an expression
using sage. Let us obtain the Taylor expansion of ``(x + 1)^n`` up to
-degree 4 about 0.::
+degree 4 about 0.
+::
var('x n')
taylor((x+1)^n, x, 0, 4)
@@ -93,27 +101,97 @@
matrix ``matrix([[1,2],[3,4]])`` and v is the vector
``vector([1,2])``.
-To solve the equation, ``Ax = v`` we simply say::
+To solve the equation, ``Ax = v`` we simply say
+::
x = solve_right(A, v)
-To solve the equation, ``xA = v`` we simply say::
+To solve the equation, ``xA = v`` we simply say
+::
x = solve_left(A, v)
The left and right here, denote the position of ``A``, relative to x.
-
+#[Puneeth]: any suggestions on what more to add?
Now, let us look at Graph Theory in Sage.
-Graph: G = Graph({0:[1,2,3], 2:[4]})
-Directed Graph: DiGraph(dictionary)
-Graph families: graphs. tab
-Invariants: G.chromatic polynomial(), G.is planar()
-Paths: G.shortest path()
-Visualize: G.plot(), G.plot3d()
-Automorphisms: G.automorphism group(), G1.is isomorphic(G2), G1.is subgraph(G2)
+We shall look at some ways to create graphs and some of the graph
+families available in Sage.
+
+The simplest way to define an arbitrary graph is to use a dictionary
+of lists. We create a simple graph by
+::
+
+ G = Graph({0:[1,2,3], 2:[4]})
+
+We say
+::
+
+ G.show()
+
+to view the visualization of the graph.
+
+Similarly, we can obtain a directed graph using the ``DiGraph``
+function.
+::
+
+ G = DiGraph({0:[1,2,3], 2:[4]})
+
+
+Sage also provides a lot of graph families which can be viewed by
+typing ``graph.<tab>``. Let us obtain a complete graph with 5 vertices
+and then show the graph.
+::
+
+ G = graphs.CompleteGraph(5)
+
+ G.show()
+
+
+Sage provides other functions for Number theory and
+Combinatorics. Let's have a glimpse of a few of them.
+
+
+::
-Now let us look at bits and pieces of Number theory, combinatorics,
+ prime_range(100, 200)
+
+gives primes in the range 100 to 200.
+
+::
+
+ is_prime(1999)
+
+checks if 1999 is a prime number or not.
+
+::
+
+ factor(2001)
+
+gives the factorized form of 2001.
+
+::
+ C = Permutations([1, 2, 3, 4])
+ C.list()
+
+gives the permutations of ``[1, 2, 3, 4]``
+
+::
+
+ C = Combinations([1, 2, 3, 4])
+ C.list()
+
+gives all the combinations of ``[1, 2, 3, 4]``
+
+That brings us to the end of this session showing various features
+available in Sage.
+
+{{{ Show summary slide }}}
+
+We have looked at some of the functions available for Linear Algebra,
+Calculus, Graph Theory and Number theory.
+
+Thank You!