On Sun, Dec 11, 2011 at 5:53 PM, Jonathan <[email protected]> wrote:
> I was inspired by the discussion here(http://groups.google.com/group/
> sage-devel/browse_thread/thread/e134f0b9292bc65c/49606b41906469d5?
> lnk=gst&q=Javascript+spreadsheet+in+Sage+worksheet#49606b41906469d5)
> and an assignment I give regularly which requires checking the
> numerical results my students generate for data sets after doing some
> messy algebra (potentially different each time).
>
> Here is a first pass at a function that generates an interact into
> which you can enter columns of data and have them used as the input
> into an expression to calculate numerical results.  This should
> probably be converted to a special version of an interact, but as
> proof of principle this is a start.  Any comments?

Can you please post your code at http://pastebin.com/ or something,
since putting it in email results in it getting all mangled by some
email clients?

Thanks!

William

>
> Jonathan
> -----------------
>
> def calctable(exprID,nrows):
>    """
> This function creates an interactive table (mini spreadsheet) with
> columns into
> which you can enter multiple values for each of the variables in the
> function with
> the name exprID (pass as a string).  A table is then created with the
> values for
> the variables plus the calculated value of the function in the last
> column.
>
> exprID:  the string name of the expression (eg. V in case of V=n*R*T/
> p)
> nrows: # of rows, different values, to enter for calculations.
>
>
> """
>
>    expr = eval(exprID)
>    html('$'+exprID+'='+latex(expr)+'$')
>    all_columns = []
>    for i in range (0,len(expr.args())):
>        all_columns.append(expr.args()[i])
>    all_columns.append(exprID)
>    ncol = len(all_columns)
>    inputcol = ncol-1
>    lastvarcol = ncol-2
>    td_label = ''
>    for i in range (0,inputcol):
>        td_label = td_label+'<td width=141; style=\'text-align:right;
> \'>'+str(all_columns[i])+' </td>'
>    from time import time
>    uniqueID = str(int(round(time())))
>    initdata =[]
>    for i in range(0,nrows):
>        initdata.append([0]*inputcol)
>    argstr ='names=text_control(value="<table><tbody><tr>'+td_label+'</
> tr></tbody></table>")'
>    argstr = argstr+',data=input_grid('+str(nrows)+','+str(inputcol)
> +', label="", width = 10, default = initdata)'
>    argstr = argstr+',calc=checkbox(label="Check to calculate '+exprID
> +'", default=False) '
>    functionname = 'calctable'+uniqueID
>    functionstr = 'def '+functionname+'('+argstr+'):\n'
>    functionstr = functionstr+'\tall_columns='+str(all_columns)+'\n'
>    functionstr = functionstr+'\tresults = [0]*'+str(nrows)+'\n'
>    functionstr = functionstr+'\tif calc:\n'
>    for k in range (0,nrows):
>        callstr='results['+str(k)+']='+exprID+'('
>        for i in range(0,lastvarcol):
>            callstr=callstr+str(all_columns[i])+'=data['+str(k)+']
> ['+str(i)+'],'
>        callstr = callstr+str(all_columns[lastvarcol])+'=data['+str(k)
> +']['+str(lastvarcol)+'])'
>        functionstr = functionstr +'\t\t'+ callstr + '\n'
>    exprstr = str(exprID)+'='+str(expr)
>    functionstr = functionstr+'\t\thtml(\"<span>'+exprstr+'</span>\")
> \n'
>    functionstr = functionstr+'\t\thtml(\'<table style=\"border-
> collapse:collapse;\"><tbody >\')\n'
>    functionstr = functionstr+'\t\thtml(\'<tr style=
> \"background:yellow; text-align:center;\">\')\n'
>    functionstr = functionstr+'\t\tfor k in range (0,'+str(ncol)+'):
> \n'
>    functionstr = functionstr+'\t\t\thtml(\'<td style=\"border-
> style:solid; border-width:1px; border-color:lightgrey;border-
> collapse:collapse;\">\'+str(all_columns[k])+\'</td>\')\n'
>    functionstr = functionstr+'\t\thtml(\'</tr>\')\n'
>    functionstr = functionstr+'\t\tfor i in range (0,'+str(nrows)+'):
> \n'
>    functionstr = functionstr+'\t\t\tif (i%2 ==0):\n'
>    functionstr = functionstr+'\t\t\t\thtml(\'<tr>\')\n'
>    functionstr = functionstr+'\t\t\tif (i%2!=0):\n'
>    functionstr = functionstr+'\t\t\t\thtml(\'<tr style=
> \"background:lightcyan\">\')\n'
>    functionstr = functionstr+'\t\t\tfor k in range (0,'+str(inputcol)
> +'):\n'
>    functionstr = functionstr+'\t\t\t\thtml(\'<td style=\"border-
> style:solid; border-width:1px; border-color:lightgrey;border-
> collapse:collapse;\">\'+str(data[i][k])+\'</td>\')\n'
>    functionstr = functionstr+'\t\t\thtml(\'<td style=\"border-
> style:solid; border-width:1px; border-color:lightgrey;border-
> collapse:collapse;\">\'+str(results[i])+\'</td>\')\n'
>    functionstr = functionstr+'\t\t\thtml(\'</tr>\')\n'
>    functionstr = functionstr+'\t\thtml(\'</tbody><table>\')\n'
> #    print functionstr
>    exec(functionstr)
>    eval('interact('+functionname+')')
>
> ---------
> Example usage
>>var('n R T p')
>>V = n*R*T/p
>>calctable('V',4)
>
> --
> To post to this group, send an email to [email protected]
> To unsubscribe from this group, send an email to 
> [email protected]
> For more options, visit this group at 
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org



-- 
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to