Ask Your Question

tcfisher's profile - activity

2021-05-12 00:58:18 +0200 received badge  Notable Question (source)
2017-03-20 15:37:41 +0200 received badge  Popular Question (source)
2014-01-31 21:49:13 +0200 received badge  Famous Question (source)
2013-09-17 11:03:04 +0200 received badge  Great Question (source)
2013-05-26 13:50:51 +0200 received badge  Good Answer (source)
2013-03-21 07:23:04 +0200 received badge  Famous Question (source)
2012-07-08 05:05:24 +0200 received badge  Notable Question (source)
2012-03-03 09:27:48 +0200 received badge  Popular Question (source)
2012-02-27 15:48:20 +0200 received badge  Taxonomist
2012-01-17 08:08:51 +0200 received badge  Self-Learner (source)
2012-01-17 08:08:51 +0200 received badge  Nice Answer (source)
2012-01-16 15:22:31 +0200 received badge  Good Question (source)
2012-01-16 15:22:31 +0200 received badge  Nice Question (source)
2011-11-28 09:40:56 +0200 received badge  Nice Question (source)
2011-11-18 16:18:08 +0200 marked best answer How do I create a log plot of line data?

You could rescale the data before you plot it:

sage: data = [(t^2,t^3) for t in srange(1,10,.1)]
sage: L = line2d(data)
sage: L.show(figsize=[5,2])

linear plot

sage: log_data = [(log(x[0]),log(x[1])) for x in data]
sage: M = line2d(log_data)
sage: M.show(figsize=[5,2])

log plot

If you want the tick marks to be logarithmic, the docstring for M.show describes an option for using a matplotlib tick "locator".

sage: from matplotlib import ticker
sage: tloc = lambda m: ticker.LogLocator(subs=[m])
sage: M.show(figsize=[5,2],ticks=(tloc(4),tloc(1)))

log plot with log ticks

To be honest though, I had a lot of trouble using the LogLocator -- it doesn't seem completely polished, and may take a while to understand.

2011-11-18 16:17:42 +0200 marked best answer Symbolic linear algebra

Hi tcfisher,

Here's one less-tedious way, using Python's list comprehension, string formatting, and an alternate format for the matrix command:

sage: plist = ['p%s%s'%(i,j) for i in range(4) for j in range(4)]
sage: plist
['p00', 'p01', 'p02', 'p03', 'p10', 'p11', 'p12', 'p13', 'p20', 'p21', 'p22', 'p23', 'p30', 'p31', 'p32', 'p33']

sage: m = matrix(SR,4,4,plist); m
[p00 p01 p02 p03]
[p10 p11 p12 p13]
[p20 p21 p22 p23]
[p30 p31 p32 p33]

Note that this does not inject the variables 'pij' into the global namespace, so trying to write them directly will result in an error:

sage: p00
Traceback (most recent call last):
...
NameError: name 'p00' is not defined

But if all you want to do is manipulate these matrices, that will work fine:

sage: m^2
[  p00^2 + p01*p10 + p02*p20 + p03*p30 p00*p01 + p01*p11 + p02*p21 + p03*p31 p00*p02 + p01*p12 + p02*p22 + p03*p32 p00*p03 + p01*p13 + p02*p23 + p03*p33]
[p00*p10 + p10*p11 + p12*p20 + p13*p30   p01*p10 + p11^2 + p12*p21 + p13*p31 p02*p10 + p11*p12 + p12*p22 + p13*p32 p03*p10 + p11*p13 + p12*p23 + p13*p33]
[p00*p20 + p10*p21 + p20*p22 + p23*p30 p01*p20 + p11*p21 + p21*p22 + p23*p31   p02*p20 + p12*p21 + p22^2 + p23*p32 p03*p20 + p13*p21 + p22*p23 + p23*p33]
[p00*p30 + p10*p31 + p20*p32 + p30*p33 p01*p30 + p11*p31 + p21*p32 + p31*p33 p02*p30 + p12*p31 + p22*p32 + p32*p33   p03*p30 + p13*p31 + p23*p32 + p33^2]

And of course if you need the 'pij' in the global namespace, you can always declare the variables by looping over plist (or maybe something more clever, if you give it some thought, but I haven't really :)

sage: for p in plist:
....:     var(p)
....:     
p00
p01
p02
p03
p10
p11
p12
p13
p20
p21
p22
p23
p30
p31
p32
p33
sage: p00
p00
sage: p00 + 2*p13
p00 + 2*p13
2011-11-18 16:16:46 +0200 received badge  Scholar (source)
2011-11-18 16:16:46 +0200 marked best answer Is there a way to set arbitrary function commuting rules?

Here is how you can define an additive function:

sage: import operator
sage: def f_eval(self, arg):
....:     try:
....:         op = arg.operator()
....:     except AttributeError:
....:         return None
....:     if op is operator.add:
....:         return sum(map(self, arg.operands()))
....:     
sage: f = function('f',eval_func=f_eval)
sage: var('x,y')
(x, y)
sage: f(x+y)
f(x) + f(y)
sage: f(x*y)
f(x*y)

The documentation you get from

sage: function?

isn't very helpful, but this has some examples:

sage: sage.symbolic.function_factory.function?
2011-11-18 15:02:39 +0200 received badge  Supporter (source)
2011-11-18 03:31:32 +0200 received badge  Nice Question (source)
2011-11-17 15:13:21 +0200 asked a question Is there a way to set arbitrary function commuting rules?

I am trying to perform a derivation using discrete difference operations, but I don't want to actually define my discrete operator. What I would like to do, is specify

d = function('d')
var('x y')

I'd like to specify d(xy) = d(xy) (multiplication does not commute), but I need d(x+y) = d(x) + d(y) (addition does commute). Any ideas?

2011-11-17 14:45:40 +0200 received badge  Notable Question (source)
2011-07-27 09:59:41 +0200 received badge  Editor (source)
2011-07-26 18:54:24 +0200 asked a question How do I create a log plot of line data?

Is there a good way to create a log-log plot of line data in sage? I can't find it through the plot or line interfaces.

2011-04-14 16:36:29 +0200 received badge  Popular Question (source)
2010-12-01 11:15:57 +0200 answered a question Is there a way to simplify_full and trig_reduce a matrix?

I have to do this often, so I wrote a function:

def matrix_full_simplify(mat,m,n):
    matsimp=mat;
    for i in range(m):
        for j in range(n):
             matsimp[i,j]=mat[i,j].full_simplify();
    return matsimp;

Use:

amat = matrix_full_simplify(amat)

You could easily adapt this to be better or require fewer arguments and can create a similar function for vectors.

2010-10-14 23:11:12 +0200 received badge  Teacher (source)
2010-10-14 23:11:12 +0200 received badge  Student (source)
2010-09-30 12:28:21 +0200 answered a question Symbolic linear algebra

Thanks for your help niles. Here is the function I came up with to do this automatically from now on.

 def symbolic_matrix(root,m,n):
     mlist=[];
     for i in range(m):
         for j in range(n):
             mlist.append(root+'_'+str(i)+'_'+str(j));
             var(root+'_'+str(i)+'_'+str(j));
     return matrix(SR,m,n,mlist);

Note that root must be a string.

sage: pmat=symbolic_matrix('p',5,4)
sage: pmat
[p_0_0 p_0_1 p_0_2 p_0_3]
[p_1_0 p_1_1 p_1_2 p_1_3]
[p_2_0 p_2_1 p_2_2 p_2_3]
[p_3_0 p_3_1 p_3_2 p_3_3]
[p_4_0 p_4_1 p_4_2 p_4_3]
2010-09-30 10:22:36 +0200 asked a question Symbolic linear algebra

Most of my research is in numerical methods/numerical analysis. I need to perform derivations using arbitrary matrices. Currently, when using sage, I have to declare all the elements of a symbolic matrix:

var('p00 p01 p02 p03 p10 p11 p12 p13 p20 p21 p22 p23 p30 p31 p32 p33') Pmat=matrix([[p00,p01,p02,p03],[p10,p11,p12,p13],[p20,p21,p22,p23],[p30,p31,p32,p33]])

Clearly for large matrices this is prohibitively tedious. In Mathematica, I simply use:

pmat=Array[p,{4,4}]

I am then able to put constraints on the elements of p and solve for the values for the given method I am trying to derive. I prefer to use sage over Mathematica, so if someone can enlighten me how to accomplish this task in sage, I would appreciate it.