Substitute variable with value, but don't evaluate - maxima

Suppose I have the following expressions:
(%i1) (8*x)*(log(x) / log(10));
(%i2) X^2;
Now, because I want to find out what constant value I can pick to make the statement %i1 is O(%i2) true, I evaluate them in a loop like so:
for a:1 thru 10 do print(%i1, "=", ev(%i1, x=a), %i2, "=", ev(%i2, numer, x=a));
The output is:
8 x log(x) 2
---------- = 0.0 , x = 1
log(10)
8 x log(x) 2
---------- = 4.816479930623698 , x = 4
log(10)
8 x log(x) 2
---------- = 11.45091011327189 , x = 9
log(10)
8 x log(x) 2
---------- = 19.26591972249479 , x = 16
log(10)
8 x log(x) 2
---------- = 27.95880017344075 , x = 25
log(10)
8 x log(x) 2
---------- = 37.35126001841489 , x = 36
log(10)
8 x log(x) 2
---------- = 47.32549024079837 , x = 49
log(10)
8 x log(x) 2
---------- = 57.79775916748438 , x = 64
log(10)
8 x log(x) 2
---------- = 68.70546067963139 , x = 81
log(10)
8 x log(x) 2
---------- = 80.0 , x = 100
log(10)
I want to make the output easier to eyeball, something like:
8 1 log(1) 2
---------- = 0.0 , 1 = 1
log(10)
8 2 log(2) 2
---------- = 4.816479930623698 , 2 = 4
log(10)
8 3 log(3) 2
---------- = 11.45091011327189 , 3 = 9
log(10)
[snip]
8 10 log(10) 2
---------- = 80.0 , 10 = 100
log(10)
How can I tell Maxima to substitute the value of a for x in every iteration of the loop without evaluating the expression?
I've searched the manual, but I didn't find anything seemingly relevant.

A lot of operations in Maxima are carried out by a process called "simplification", which means applying identities to make a "simpler" expression. E.g. 1 + 1 simplifies to 2, sin(0) simplifies to 0, etc.
In order to get the effect you want, we must disable simplification in general, so that expressions are evaluated but not simplified. But to get the numerical values, we need to enable simplification just for those results.
Here's something to do that.
(%i16) simp : false $
(%i17) for x in [1,2,3,4,5]
do print (ev(%i1) = ev(%i1, simp, numer), ev(%i2) = ev(%i2, simp));
log(1) 2
(8 1) (-------) = 0.0 1 = 1
log(10)
log(2) 2
(8 2) (-------) = 4.816479930623698 2 = 4
log(10)
log(3) 2
(8 3) (-------) = 11.4509101132719 3 = 9
log(10)
log(4) 2
(8 4) (-------) = 19.26591972249479 4 = 16
log(10)
log(5) 2
(8 5) (-------) = 27.95880017344075 5 = 25
log(10)
(%o17) done
Note that I wrote for x in [1, 2, 3, 4, 5] ... instead of for x:1 thru 5 .... That's because the latter uses arithmetic, which requires simplification. Try it both ways, I think you'll see the difference, and it is very enlightening, I believe.
Nota bene I've used the same values of %i1 and %i2 as you.

Use "empty" function:
(%i1) display2d: false $
(%i2) prefix("") $
(%i3) almost_subst(a, x, e):= subst(""(a), x, e) $
(%i4) almost_subst(10, x, 8*x*log(x)/log(10));
(%o4) (8* 10*log( 10))/log(10)

Related

Simplify equation after solving

In Maxima I put:
q_d: 1000-5*p_d;
q_s: -250+2*p_s;
p_d: (1+t)*p_s;
eq:q_d = q_s;
solve(eq,p_s);
EC: 10*q_d + 0.01 * (q_d**2);
get the result
p_s=1250/(5*t+7)
0.01*(1000-5*p_s*(t+1))^2+10*(1000-5*p_s*(t+1))
How do I further simplify EC in term of 't' only?
One way to go about this is to express all the relations you listed as equations, and then solve the equations for the variables you want to eliminate, then you get expressions in terms of t which you can substitute into EC to get a result only in terms of t.
(%i2) e1: q_d = 1000-5*p_d;
(%o2) q_d = 1000 - 5 p_d
(%i3) e2: q_s = -250+2*p_s;
(%o3) q_s = 2 p_s - 250
(%i4) e3: p_d = (1+t)*p_s;
(%o4) p_d = p_s (t + 1)
(%i5) e4: q_d = q_s;
(%o5) q_d = q_s
(%i6) solns: solve ([e1, e2, e3, e4], [q_d, q_s, p_d, p_s]);
1250 t - 750 1250 t - 750
(%o6) [[q_d = - ------------, q_s = - ------------,
5 t + 7 5 t + 7
1250 t + 1250 1250
p_d = -------------, p_s = -------]]
5 t + 7 5 t + 7
Now in %o6 I have a list of equations for the variables to be eliminated.
(%i7) EC: 10*q_d + 0.01 * (q_d**2);
2
(%o7) 0.01 q_d + 10 q_d
I'll substitute into EC to get a result in terms of t only.
(%i8) subst (solns[1], EC);
2
0.01 (1250 t - 750) 10 (1250 t - 750)
(%o8) -------------------- - -----------------
2 5 t + 7
(5 t + 7)
I'll use ratsimp to simplify the result.
(%i9) ratsimp (%);
rat: replaced 0.01 by 1/100 = 0.01
2
46875 t + 68750 t - 58125
(%o9) - --------------------------
2
25 t + 70 t + 49

Create a polymer chain of nonstandard residues from a single residue pdb

I created a simple PDB file with a non-standard residue of repeat unit of polyethylene glycol (CH2-O-CH2) as follows
REMARK Materials Studio PDB file
REMARK Created: Mon Dec 04 09:52:49 2017
ATOM 1 CT1 EGR H 1 -14.882 2.339 0.134 1.00 0.00 C
ATOM 2 HC11 EGR H 1 -14.677 2.559 1.234 1.00 0.00 H
ATOM 3 HC12 EGR H 1 -14.774 3.298 -0.472 1.00 0.00 H
ATOM 4 OS1 EGR H 1 -13.892 1.317 -0.371 1.00 0.00 O
ATOM 5 CT2 EGR H 1 -12.493 1.852 -0.184 1.00 0.00 C
ATOM 6 HC21 EGR H 1 -12.292 2.009 0.928 1.00 0.00 H
ATOM 7 HC22 EGR H 1 -12.392 2.846 -0.732 1.00 0.00 H
TER 8
CONECT 1 2 3 4
CONECT 2 1
CONECT 3 1
CONECT 4 1 5
CONECT 5 4 7 8 6
CONECT 6 5
CONECT 7 5
END
I'm able to read this pdb file successfully using the bioPDB class using the following code
parser = PDBParser()
structure = parser.get_structure('EGR', pdb_file)
How to use this structure object to create a pdb file of a polymer chain of `'n' residues?
Let's say you want to replicate 10 times your residue over the x-axis with a gap of 5 angstroms between each residue. You could try something like:
import numpy as np
from Bio.PDB import PDBParser
from Bio.PDB.Residue import Residue
from Bio.PDB.Atom import Atom
parser = PDBParser()
io = PDBIO()
structure = parser.get_structure('EGR', pdb_file)
chain = list(structure.get_chains())[0]
atoms = list(structure.get_atoms())
serial_number = len(atoms)
gap = 5.0
for resnum in range(10):
resnum += 2 # position along the sequence
res_id = ('', resnum, '')
res_name = "EGR" + str(resnum) # define name of residue
res_segid = ' '
new_res = Residue(res_id, res_name, res_segid)
chain.add(new_res)
for atom in atoms:
serial_number += 1
atom_name = atom.name
atom_coord = atom.coord + [gap * (resnum + 1), 0, 0]
atom_bfactor = atom.bfactor
atom_occ = atom.occupancy
atom_altloc = atom.altloc
atom_fullname = atom.fullname
atom_serial = serial_number
atom_element = atom.element
new_atom = Atom(atom_name, atom_coord, atom_bfactor, atom_occ, atom_altloc, atom_fullname, atom_serial, element=atom_element)
new_res.add(new_atom)

Is there a way to use Machine Learning classify discrete and infinite scale data?

The data like that:
x y
7773 0
9805 4
7145 0
7645 1
2529 1
4814 2
6027 2
7499 2
3367 1
8861 5
9776 2
8009 5
3844 2
1218 2
1120 1
4553 0
3017 1
2582 2
1691 2
5342 0
...
The real function f(x) is: (Return the circle count of a decimal integer)
# 0 1 2 3 4 5 6 7 8 9
_f_map = [1, 0, 0, 0, 0, 0, 1, 0, 2, 1]
def f(x):
x = int(x)
assert x >= 0
if x == 0:
return 1
r = 0
while x:
r += _f_map[x % 10]
x /= 10
return r
The training data and test data can be produced by random:
data = []
target = []
for i in xrange(3000):
x = random.randint(0, 999999) #hardcode a scale
data.append([x])
target.append(f(x))
The real function is discrete and infinite scale.
Is there a way or a model can classify this data?
I tried SVM(Support Vector Machine), and acquired a 20% accuracy rate.
Looks like a typical use case of sequential models. You can easily learn LSTM/ other recurrent neural network to do so by considering your numbers as sequences of integers feeded to the network. At this point it just has to learn sum operation and a simple mapping(your f_map).

Estimation of Surface Normal in a Dense Range Image

I am trying to implement the surface normal estimation proposed by Hinterstoisser et al (2011) but I'm not clear with some points:
In equation (9), is D(x) corresponding to the depth value (Z-axis) at pixel location x?
How to estimate the value of the gradient ▽D using 8 neighboring points around the point of interest?
As mentioned, D is a dense range image meaning that for any pixel location x in D where x = [x y]T, D(x) is the depth at pixel location x (or simply D(x, y)).
Estimating the optimal Gradient in a least-square sense
Suppose we have the following neighborhood around the depth value 5 in D(x) for some x:
8 1 6
3 5 7
4 9 2
Then, using the Taylor expansion
dxT.grad(x) + error = D(x + dx) - D(x)
we get eight equations for the neighborhood points
[1 0]g + e = 7 - 5
[-1 0]g + e = 3 - 5
[0 1]g + e = 9 - 5
[0 -1]g + e = 1 - 5
[1 1]g + e = 2 - 5
[1 -1]g + e = 6 - 5
[-1 1]g + e = 4 - 5
[-1 -1]g + e = 8 - 5
that we can represent in matrix form Ag + e = b as
[1 0;-1 0;0 1;0 -1;1 1;1 -1;-1 1;-1 -1]g + e= [2;-2;4;-4;-3;1;-1;3]
Then minimize the squared error
||Ag - b||22. The analytical solution for g^ that minimizes this error is of the form
g^ = (ATA)-1ATb

Calculated nCr mod m (n choose r) for large values of n (10^9)

Now that CodeSprint 3 is over, I've been wondering how to solve this problem. We need to simply calculate nCr mod 142857 for large values of r and n (0<=n<=10^9 ; 0<=r<=n). I used a recursive method which goes through min(r, n-r) iterations to calculate the combination. Turns out this wasn't efficient enough. I've tried a few different methods, but they all seem to not be efficient enough. Any suggestions?
For non-prime mod, factor it (142857 = 3^3 * 11 * 13 * 37) and compute C(n,k) mod p^q for each prime factor of the mod using the general Lucas theorem, and combine them using Chinese remainder theorem.
For example, C(234, 44) mod 142857 = 6084, then
C(234, 44) mod 3^3 = 9
C(234, 44) mod 11 = 1
C(234, 44) mod 13 = 0
C(234, 44) mod 37 = 16
The Chinese Remainder theorem involves finding x such that
x = 9 mod 3^3
x = 1 mod 11
x = 0 mod 13
x = 16 mod 37
The result is x = 6084.
Example
C(234, 44) mod 3^3
First convert n, k, and n-k to base p
n = 234_10 = 22200_3
k = 44_10 = 1122_3
r = n-k = 190_10 = 21001_3
Next find the number of carries
e[i] = number of carries from i to end
e 4 3 2 1 0
1 1
r 2 1 0 0 1
k 1 1 2 2
n 2 2 2 0 0
Now create the factorial function needed for general Lucas
def f(n, p):
r = 1
for i in range(1, n+1):
if i % p != 0:
r *= i
return r
Since q = 3, you will consider only three digits of the base p representation at a time
So
f(222_3, 3)/[f(210_3, 3) * f(011_3, 3)] *
f(220_3, 3)/[f(100_3, 3) * f(112_3, 3)] *
f(200_3, 3)/[f(001_3, 3) * f(122_3, 3)] = 6719344775 / 7
Now
s = 1 if p = 2 and q >= 3 else -1
Then
p^e[0] * s * 6719344775 / 7 mod 3^3
e[0] = 2
p^e[0] = 3^2 = 9
s = -1
p^e[0] * s * 6719344775 = -60474102975
Now you have
-60474102975 / 7 mod 3^3
This is a linear congruence and can be solved with
ModularInverse(7, 3^3) = 4
4 * -60474102975 mod 27 = 9
Hence C(234, 44) mod 3^3 = 9

Resources