Substituting variable value in sage - sage

I use the following code to calculate the Kretschmann scalar of the Schwartzchild spacetime:
print("Initialization of manifold, chart, and metric. Definitions of constants.")
M=Manifold(4,'M',structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
m=var('m')
g=M.metric()
g[0,0]=-(1-2*m/r)
g[1,1]=1/(1-2*m/r)
g[2,2]=r^2
g[3,3]=r^2*sin(th)^2
print('Calculating Riemann tensor')
R = g.riemann()
print(R[:])
uR=R.up(g)
dR=R.down(g)
print('Calculating Kretschmann scalar')
kr= uR['^{abcd}']*dR['_{abcd}']
print(kr.expr())
It works (I get the correct symbolic expression), but now I want to substitute a value for the variable m. I have tried with:
kr.substitute(m==10)
kr.subs(m==10)
kr.substitute_expression(m==10)
but none of these work. How can I make the substitution m=10?

I have found the solution myself: one has to consider the expression of kr before substituting:
kr.expr().subs(m=10)

Related

How to code exponential numbers with powers in R?

I want to multiply a this number 4.193215e+12 with a dataframe. My code is
df <- cbind(Dataset = df$Dataset, df[,2:4] * 4.193215e^12
However an error appears. What is the proper way to code this number 4.193215e+12 in R?
While this is found in the not-quite-obvious location ?NumericConstants , I am hard-pressed to think of a language in which Xe^Y is syntactically correct. Always use either e or ^ for powers.

Levene test in R

I am Having a little problem doing a Levene test in R. I does not get any output value, only NaN. Anyone know what the problem might be?
Have used the code:
with(Test,levene.test(Sample1,Sample2,location="median"))
The problem
Best regards
The levene.test function assumes the data are in a single vector. The second argument is a grouping variable.
Concatenate your data using the c() function: data=c(Sample1, Sample2). Construct a vector of group names like gp = rep('Gp1','Gp2', each=240). Then, call the function as follows: levene.test(data, gp, location='median').
This can also be done directly:
levene.test(c(Sample1, Sample2), rep('Gp1', 'Gp2', each=240)), location='median')

Limit in R in terms of another variable

I need to take a limit of a function $\frac{x^n-1}{x-1}$ in R but I want the answer in terms of n. I tried defining n as a symbol but it did not work. I am new to R so I would appreciate any assistance.
Assuming that you want to take the limit of this function for x->1, you can obtain the result by using the package Ryacas in the following way:
require(Ryacas)
x <- Sym("x")
n <- Sym("n")
Limit((x^n-1)/(x-1),x,1)
which yields the answer:
expression(n)

maple: plotting result of a numerical dsolve

I have to solve a differential equation numerically; so to say:
diff(y(x), x)+x^2-15*x = 5
with the initial conditions:
inc := y(0) = 0
the solution is of course:
sol := dsolve({f, inc}, numeric);
which results in:
proc(x_rkf45) ... end
Now I want to plot y(x) for x=0..2 for instance.
What shoudl I do?
the code:
plot(sol(x), x = 0 .. 2);
does not work!
Warning, unable to evaluate the function to numeric values in the
region; see the plotting command's help page to ensure the calling
sequence is correct
Here are three different ways to do that.
The first is to use the DEtools[DEplot] command, which both solves and plots. It's input is the differential equation(s) and one or more sets of intitial conditions (as opposed to something that dsolve(...,numeric) returns).
The DEplot command has lots of options. You can turn off inclusion of the field plot, for example.
restart:
deq := diff(y(x), x)+x^2-15*x = 5:
ic := y(0) = 0:
DEtools[DEplot](deq, y(x), x=0..2, [ic]);
The next way is to call dsolve(...,numeric) as you did, and to pass what it returns to the plots:-odeplot command.
sol := dsolve({deq, ic}, numeric):
plots:-odeplot(sol, x=0..2);
Yet another way is to pass dsolve the additional output=listprocedure option so that it returns a list of procedures. Any of those can then be extracted and used to compute at a point or used by passing to the usual plot command.
sollist := dsolve({deq, ic}, numeric, output=listprocedure):
Y := eval(y(x),sollist):
Y(1.0);
12.1666666666667
plot( Y, 0..2 );
See the help pages for DEtools[DEplot], plots:-odeplot or plot,options for more details on customizing the resulting plots.
If you choose to go the odeplot way and also wish to include the field plot then you can augment the plot using plots:-display and plots:-fieldplot.

R tree() / maptree() values for categorical splits

I am trying to get a more meaningful version of the data plotted when a categorical predictor appears in the output of a tree function.
The values are airport codes: FLR, FUE, GOA, HER etc,
If I use tree() and
plot(Simulate.tree2); text(Simulate.tree2, pretty=1)
I get:
Which is not bad, but the codes are abbreviated and not clear.
If I use maptree() and
draw.tree(Simulate.tree2)
I get:
which is not at all helpful, since the letters just indicate the position of the value in a vector (I assume)
Is there a way in either package (or both) to get the actual values printed?
Have you tried this?
plot(Simulate.tree2)
text(Simulate.tree1, pretty = 3)
From the documentation, it looks like passing an integer to pretty sets the minimum length of the labels at that integer value. So for airport codes, you'd want 3.

Resources