Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanai committed Apr 21, 2017
0 parents commit 22d049c
Show file tree
Hide file tree
Showing 13 changed files with 15,765 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# 1. Generate Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## 1.1 Determinant\n",
"Here we generate a square matrix $A$ of dimension $n$ whose:\n",
"* Entries are $\\mathbb{Z}$ between 6 and 12\n",
"* $100<\\vert\\det(A)\\vert <150 $\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"def generate_det(n,lb=6,ub=12,dlb=100,dub=150):\n",
" control=0\n",
" while control==0:\n",
" A=random_matrix(ZZ, n,x=lb,y=ub);\n",
" d=det(A)\n",
" if dlb<abs(d)<dub:\n",
" control=1\n",
" return(A,d)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"(\n",
"[11 11 6 9 9] \n",
"[ 9 9 9 8 10] \n",
"[ 9 9 9 8 8] \n",
"[ 8 6 11 10 8] \n",
"[ 9 10 6 7 10], -122\n",
")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_det(4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## 1.2 Inverse \n",
"Here we generate a square matrix $A$ of dimension $n$ whose:\n",
"* Entries are $\\mathbb{Z}$ between 1 and 5\n",
"* $2<\\vert\\det(A)\\vert <8 $\n",
"\n",
"Finally $A^{-1}$ contains entries in $\\mathbb{Q}-\\mathbb{Z}$ only."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"def generate_inverse(n,lb=1,ub=5,det_min=2,det_max=8):\n",
" control=0\n",
" while control==0:\n",
" A=random_matrix(ZZ, n,x=lb,y=ub);\n",
" control=A.determinant()\n",
" if control!=0 and det_min<abs(control)<det_max:\n",
" B=A.inverse()\n",
" if any(x in ZZ for x in B.list()):\n",
" control=0\n",
" else:\n",
" control=0\n",
" return(A,B)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"(\n",
"[1 1 2] [ 1/3 -1/3 2/3]\n",
"[4 3 4] [-8/3 5/3 -4/3]\n",
"[3 1 1], [ 5/3 -2/3 1/3]\n",
")"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_inverse(3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## 1.3 Eigenvalues and Eigenvectors\n",
"\n",
"Here we generate a square matrix $A$ of dimension $n$ with $n$ distinct eigenvalues.\n",
"\n",
"* The eigenvalues $\\lambda$ are randomly choosen from a list, sign is random too.\n",
"* The entries $A_{i,j}$ are in $\\mathbb{ZZ}$ and $2\\leq\\vert A_{i,j}\\vert \\leq 10$ "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"def generate_eigenvalues(n,lst):\n",
" if len(lst)<n:\n",
" print(\"ERROR: insert a bigger list!\")\n",
" else:\n",
" shuffle(lst)\n",
" return([choice([-1,1])*el for el in lst[:n]])\n",
"\n",
"def diagonalizable(n,lst=range(2,8),lb=2,ub=10):\n",
" L=diagonal_matrix(generate_eigenvalues(n,lst))\n",
" control=0\n",
" while control==0:\n",
" Q=random_matrix(ZZ, n,x=-4,y=4) # you can modify here\n",
" control=det(Q)\n",
" if control!=0:\n",
" P=Q*L*Q.inverse()\n",
" plst=P.list()\n",
" # we want an integer matrix without 0s\n",
" if not all(x in ZZ for x in plst) or 0 in plst:\n",
" control=0\n",
" plst=[abs(x) for x in plst]\n",
" # for every entry x in A lb<=abs(a)<=ub\n",
" if min(plst)<lb or max(plst)>ub:\n",
" control=0\n",
" var('l', latex_name='\\lambda')\n",
" d=str(latex(((P-l*identity_matrix(n)).det()).expand()))\n",
" values=', '.join([str(latex(l))+\"_\"+str(idx+1)+\"=\"+str(val) \n",
" for idx, val in enumerate(P.eigenvalues())])\n",
" vecs=', '.join([\"v_{\"+str(el[0])+\"}=\"+str(latex(matrix(el[1]).transpose())) \n",
" for el in P.eigenvectors_right()])\n",
" return(P,d,values,vecs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# 2. Generate Assignments in $\\LaTeX$ with Jinja2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"with open(\"student_numbers.csv\",\"w\") as f:\n",
" f.write(\"\\n\".join(str(randint(100000,999999)) for i in range(100)))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import jinja2\n",
"import subprocess\n",
"\n",
"def generate_jinja():\n",
" \n",
" latex_jinja_env = jinja2.Environment(\n",
" block_start_string = '\\BLOCK{',\n",
" block_end_string = '}',\n",
" variable_start_string = '\\VAR{',\n",
" variable_end_string = '}',\n",
" comment_start_string = '\\#{',\n",
" comment_end_string = '}',\n",
" line_statement_prefix = '%%',\n",
" line_comment_prefix = '%#',\n",
" trim_blocks = True,\n",
" autoescape = False,\n",
" loader = jinja2.FileSystemLoader(os.path.abspath('.'))\n",
" )\n",
" \n",
" f=open('assignments.tex','w')\n",
" for idx, seed in enumerate(open('student_numbers.csv').read().split('\\n')):\n",
" set_random_seed(int(seed))\n",
" (A,d)=generate_det(4)\n",
" (B,B_inv)=generate_inverse(3)\n",
" (C,dl,values,vecs)=diagonalizable(3)\n",
" \n",
" diz={'code':'{:03}'.format(idx+1),\n",
" 'A':str(latex(A)),\n",
" 'd':str(d),\n",
" 'B':str(latex(B)),\n",
" 'B_inv':str(latex(B_inv)),\n",
" 'C':str(latex(C)),\n",
" 'dl':dl,\n",
" 'values': values,\n",
" 'vecs': vecs\n",
" }\n",
" template = latex_jinja_env.get_template('template.tex')\n",
" text=template.render(diz)\n",
" f.write(text.encode(\"UTF-8\"))\n",
" f.close()\n",
" subprocess.call(['pdflatex', 'questions.tex'])\n",
" subprocess.call(['pdflatex', 'solutions.tex'])\n",
" folder=\"PDF\"\n",
" if not os.path.exists(folder):\n",
" os.mkdir(folder)\n",
" os.rename('questions.pdf',os.path.join(folder,\"questions.pdf\"))\n",
" os.rename('solutions.pdf',os.path.join(folder,\"solutions.pdf\"))\n",
" os.chdir('..')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 30.2 s, sys: 496 ms, total: 30.7 s\n",
"Wall time: 31.9 s\n"
]
}
],
"source": [
"%time generate_jinja()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 7.6",
"language": "",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added Assignments Generator/PDF/questions.pdf
Binary file not shown.
Binary file added Assignments Generator/PDF/solutions.pdf
Binary file not shown.
Loading

0 comments on commit 22d049c

Please sign in to comment.