how do I tell sympy that a variable is known? - sympy

I am trying to solve a large system of linear equations with 32 unknown variables and 4 known variables und 34 equations using sympy.
known variables: e, j, o, t
unknown variables: a, b, c, d, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a_a, b_b, c_c, e_e, t_t, f_f, m_m, p_p, s_s, o_o
I am looking for expressions for u, v, w, y, z, c_c but only depending on the known variables.
How can I tell sypmy that e, j, o, t are known?
I tryed solving my problem like this:
import sympy as sp
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = sp.symbols(
'a b c d e f g h i j k l m n o p q r s t u v w x y z')
a_a, b_b, c_c, e_e, t_t, f_f, m_m, p_p, s_s, o_o = sp.symbols(
'a_a b_b c_c e_e t_t f_f m_m p_p s_s o_o')
eq1 = sp.Eq(a+b+c+d, e)
eq2 = sp.Eq(f+g+h+i, j)
eq3 = sp.Eq(k+l+m+n, o)
eq4 = sp.Eq(p+q+r+s, t)
eq5 = sp.Eq(a+f+k+p, 1)
eq6 = sp.Eq(b+g+l+q, 1)
eq7 = sp.Eq(c+h+m+r, 1)
eq8 = sp.Eq(d+i+n+s, 1)
eq9 = sp.Eq(1/m_m, a)
eq10 = sp.Eq(x/m_m, f)
eq11 = sp.Eq(a_a/m_m, k)
eq12 = sp.Eq(e_e/m_m, p)
eq13 = sp.Eq(u/p_p, b)
eq14 = sp.Eq(1/p_p, g)
eq15 = sp.Eq(b_b/p_p, l)
eq16 = sp.Eq(t_t/p_p, q)
eq17 = sp.Eq(v/s_s, l)
eq18 = sp.Eq(y/s_s, h)
eq19 = sp.Eq(1/s_s, m)
eq20 = sp.Eq(f_f/s_s, r)
eq21 = sp.Eq(w/o_o, d)
eq22 = sp.Eq(z/o_o, i)
eq23 = sp.Eq(c_c/o_o, n)
eq24 = sp.Eq(1/o_o, s)
eq25 = sp.Eq(x, 1/u)
eq26 = sp.Eq(a_a, 1/v)
eq27 = sp.Eq(e_e, 1/w)
eq28 = sp.Eq(b_b, 1/y)
eq29 = sp.Eq(t_t, 1/z)
eq30 = sp.Eq(f_f, 1/c_c)
eq31 = sp.Eq(1+x+a_a+e_e, m_m)
eq32 = sp.Eq(u+1+b_b+t_t, p_p)
eq33 = sp.Eq(v+y+1+f_f, s_s)
eq34 = sp.Eq(w+z+c_c+1, o_o)
solution = sp.solve((eq1, eq2, eq3, eq4, eq5, eq6,
eq7, eq8, eq9, eq10, eq11, eq12,
eq13, eq14, eq15, eq16, eq17, eq18,
eq19, eq20, eq21, eq22, eq23, eq24,
eq25, eq26, eq27, eq28, eq29, eq30,
eq31, eq32, eq33, eq34
), u, v, w, y, z, c_c)
print(solution)
The print function gives me an empty list:
[]
I expected the syntax to be correct, since I don't get any errors, but I did not find a way to tell sympy that e, j, o, t are known and that I want the solution to be dependend of the known variables e, j, o, t only.

Once you eliminate all single-letter variables (except for e,j,o,t) you end up with 10 nonlinear equations in 11 unknowns and the constraint e + j + o + t = 4 as the 11 equation. So it seems you would need an additional relationship to solve this system of equations. I replaced a_a with A, etc..., and am keeping only the numerators of your expressions and this is what I get:
[e + j + o + t - 4,
E*O*P*S + F*M*O*P - M*O*P*S*t + M*O*S*T + M*P*S,
O*P*S*(-B**2*M*O*S - B*C*M*P*S + B*E*O*P*S + B*M*O*P*S*o - B*M*O*P - B*M*O*S*T - B*M*O*S + B*O*P*S + C*M*P**2*S - C*M*P*S*T - C*M*P*S - E*O*P**2*S + E*O*P*S*T + E*O*P*S - M*O*P**2*S*o + M*O*P**2*S + M*O*P**2 + M*O*P*S*T*o - M*O*P*S*T + M*O*P*S*o - M*O*P*S - M*O*P*T - M*O*P - O*P**2*S + O*P*S*T),
S*(-B**2*M*O*S - B*C*M*P*S + B*M*O*P*S*o - B*M*O*P - O*P**2),
-B**2*E*M*O*S - B*E*F*M*O*P - B*E*M*O*P*S*e + 2*B*E*M*O*P*S - B*E*M*O*P - B*E*M*O*S*T - B*E*M*O*S + B*E*O*P*S + B*M*P*S - E*M*O*P, O*P*S*(-C*E*M*P*S*T + E**2*O*P*S*T + E*F*M*O*P*T + E*M*O*P*S*T*e + E*M*O*P*S*T*j + E*M*O*P*S*T*o - 3*E*M*O*P*S*T + E*M*O*S*T**2 - E*M*P*S - M*P*S*T),
C*F - 1,
-S*(E*M*O*P*e - E*M*O*P + E*M*O*T + E*M*O - E*O*P - M*P),
O*P*S*(E*O*P*S + F*M*O*P + M*O*P*S*e + M*O*P*S*j + M*O*P*S*o - 4*M*O*P*S + M*O*S*T + M*P*S)]

Related

Modular Divison when no Modular Multiplicative Inverse exists

I'm trying to solve the equation:
(binomial(n - 3, k, p) * binomial(n + k, n - 1, p) / (n + k)) % 1000003
My current solution:
int a, b, r;
a = BinomialModPrime(n - 3, k, P);
b = BinomialModPrime(n + k, n - 1, P);
r = ModularMultiply(a, b, P);
r = ModularMultiply(r, ModularInverse(n + k, P), P);
was working fine until I got to a problem set where there was no modular inverse for any (n + k) because
(n + k) % 1000003 == 0.
So my question is, is there another way to solve this that doesn't involve multiplication by the modular inverse? For instance, will any of these suggestions work for my problem?

Wrong Wolfram Mathematica code translation

I am pretty new to Mathematica. I need to create 2d array dynamicly. I got some code in C++(Qt) and it looks like this:
void Wave::getMatrix(int M, int N)
{
int k = -N;
while(k < N+1){
QVector<tsk_type> temp_vec;
for(int i = -N; i < N+1; i++){
tsk_type temp_sum0;
for(int ii = -M; ii < M+1; ii++ ){
temp_sum0 += (getI(i-ii, b)/getY(ii)) * getJ(ii-k,b1,b2);
}
temp_vec[temp_vec.size()-1] = temp_sum0;
if (k == i)
temp_vec[temp_vec.size()-1] -= l;
}
temp_vec.push_back(getD(S)*getI(S-k, b));
main_array.push_back(temp_vec);
k++;
}
}
/
In Mathematica, I have already written all the functions that I need to make some linear equation system matrix which I can solve. So I use 'em to get 'list' of 'lists'.
When I run this code it's like executing something but outputs nothing. No even errors.
Whats wrong? And how properly I should translate it? also, I assume that there are problem with 'List[]' and 'List[[]]' variables, so how shold I properly declare dynamic lists?
Here is the formula
Heres how I translated the code.
(*all globlas definiton is somewhere here*)
k = -Nm;
mainmatrix = List[[]];
While[k < Nm + 1,
rowvalues = List[];
For[i = -Nm, i < Nm + 1, i++,
tempsum;
For[j = -M, j < M + 1, j++,
tempsum = tempsum + (getI[i - j, a, b]/getGamma[j]) * getJ[j - k, a1, b1, a2, b]
]
AppendTo[rowvalues, tempsum];
If[k == i, AppendTo[rowvalues, -l], 0]
];
AppendTo[rowvalues, getD[S]*getI[S - k, a, b]];
AppendTo[mainmatrix, rowvalues];
k++]
UPD:
all functions and data
Clear[k, a, b, a1, b1, a2, b2, angle, rho, omega, lambda, u];
(*constants*)
rho = 1800; angle = 0.5; lambda = 3 * 10^9; omega = 1.5 * 10^6; u =
2*10^9;
BS = 1; DS = 1; T = 0.01; l = 0.01; S = 0;
a = 0.002; b = 0.008; a1 = 0.0; b1 = 0.002; a2 = 0.008; b2 = 1.0; M =
7; Nm = 7;
getI[k_ , a_, b_] = Integrate[E^(I ((2 Pi)/l) k t), {t, a, b}]
getJ[k_ , a1_, b1_, a2_, b2_] = getI[k, a1, b1] + getI[k, a2, b2]
getL[n_] = angle + (2 Pi n/l);
getK[j_] = If[j == 1,
answer = Sqrt[(rho*omega^2)/(lambda + 2*u)],
answer = Sqrt[(rho*omega^2)/(u)]
]; answer
getBeta[j_, n_] = If[(getL[n] > getK[j]),
beta = 0 + i*(getL[n]* getL[n] * getK[j]*getK[j]),
beta = (getL[n]* getL[n] * getK[j]*getK[j]) + i*0]; beta
getGamma[n_] = (((getL[n]*getL[n])/(getBeta[1, n])) + getBeta[2, n]);
getD[s_] = ((2 getL[s] * BS * getBeta[1, s] + DS) / (getL[s] * getL[s]
+ getBeta[1, s]*getBeta[2, s] ));
I don't know if all this is exactly correct, but it is much closer.
In[1]:= n = 2; M = 2; (*dummy values for this example code*)
getGamma[i_] := RandomReal[]; (* dummy functions for this example code*)
getI[i_, a_, b_] := RandomReal[];
getJ[j_, a1_, b1_, a2_, b_] := RandomReal[];
getD[S_] := RandomReal[];
k = -n;(*N is a predefined Mathematica function and can't be a variable name*)
mainmatrix = List[];
While[k < n+1,
rowvalues = List[];
For[i = -n, i < n+1, i++,
AppendTo[rowvalues, getGamma[i]];
tempsum = 0; (*I think you wanted this initialized here*)
For[j = -M, j < M+1, j++,
tempsum += getI[i-j, a, b]/getGamma[j]*getJ[j-k, a1, b1, a2, b]
];
AppendTo[rowvalues, tempsum];
If[k == i, AppendTo[rowvalues, -l]] (*no "else" needed if else does nothing*)
];
AppendTo[rowvalues, getD[S]*getI[S-k, a, b]];
AppendTo[mainmatrix, rowvalues];
k++
];
mainmatrix (*display result*)
Out[8]= {{0.135926, 0.894736, -l, 0.699663, 1.91913, 0.702762,
28.4151, 0.730135, 19.6996, 0.583233, 21.2716, 0.398302},
{0.572982, 3.18868, 0.495877, 1.50816, -l, 0.686158,
68.9278, 0.860748, 3.91516, 0.751198, 8.43028, 0.223722},
{0.931385, 3.16432, 0.931398, 5.10999, 0.241402, 4.54042,
-l, 0.825971, 2.99634, 0.280342, 3.20253, 0.0731139},
{0.294396, 7.99678, 0.456691, 4.74995, 0.308643, 1.72647,
0.883139, 5.64323, -l, 0.755833, 4.00285, 0.127718},
{0.790168, 0.751702, 0.744966, 2.40172, 0.537242, 3.08838,
0.105972, 1.09212, 0.412047, 12.2475, -l, 0.397379}}
When time comes to use the matrix result realize that all Mathematica matrix and vector subscripts are 1..n, not 0..n-1 and not -n..n. So you will have to add an offset to any matrix subscripts when modifying code to use the resulting matrix.
You could, if it mattered to you, replace
tempsum = 0;(*I think you wanted this initialized here*)
For[j = -M, j < M + 1, j++,
tempsum += getI[i - j, a, b]/getGamma[j]*getJ[j - k, a1, b1, a2, b]];
AppendTo[rowvalues, tempsum];
with
AppendTo[rowvalues,Sum[getI[i-j,a,b]/getGamma[j]*getJ[j-k,a1,b1,a2,b],{j,-M,M}]];
That might have, for example, avoided the original bug of not initializing tempsum.
There are other bits of the code that could be rewritten to make use of Mathematica features, but you have to decide whether this would be good or bad to do.
here you go without all the nasty use of AppendTo
mainmatrix = Table[
Insert[
Append[
Flatten#Table[ {
getGamma[i],
Sum[getI[i - j, a, b]/getGamma[j] getJ[j - k, a1, b1, a2, b],
{j, -M, M}] },
{i, -n, n}] ,
getD[S]*getI[S - k, a, b]],
-l,2 (k + n + 1) + 1 ],
{k, -n, n}];
Edit:
on further study, it appears the C++ code actually throws away the getY(i) value it pushes onto temp_vec, the result is even simpler:
mainmatrix = Table[
Append[
Table[ Sum[getI[i - j, a, b]/
getGamma[j] getJ[j - k, a1, b1, a2, b]-l Boole[i == k],
{j, -M, M}] , {i, -n, n}] ,
getD[S]*getI[S - k, a, b]], {k, -n, n}];
yet another form, which actually begins to resemble your equation..
lhs = Table[Sum[getI[i - j, a, b]/
getGamma[j] getJ[j - k, a1, b1, a2, b], {j, -M, M}],
{k, -n, n}, {i, -n, n}] - l IdentityMatrix[2 n + 1];
rhs = Table[ getD[S]*getI[S - k, a, b] , {k, -n, n}];
mainmatrix= Join[lhs, Transpose[{rhs}], 2]

How to understand this RayTracer code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So this RT code creates a 3D image, with blur, through raw code. How is that actually done without any modelling tools?
I am currently working to understand how RT work and different ways to implement them, so this was kind of cool to see such a small amount of code producing a pretty impressive 3D image.
#include <stdlib.h> // card > aek.ppm
#include <stdio.h>
#include <math.h>
#include <fstream>
typedef int i;
typedef float f;
struct v {
f x, y, z;
v operator+(v r) {
return v(x + r.x, y + r.y, z + r.z);
}
v operator*(f r) {
return v(x * r, y * r, z * r);
}
f operator%(v r) {
return x * r.x + y * r.y + z * r.z;
}
v() {}
v operator^(v r) {
return v(y * r.z - z * r.y, z * r.x - x * r.z, x * r.y - y * r.x);
}
v(f a, f b, f c) {x = a; y = b; z = c;}
v operator!() {
return*this * (1 / sqrt(*this % *this));
}
};
i G[] = {247570, 280596, 280600, 249748, 18578, 18577, 231184, 16, 16};
f R()
{
return(f)rand() / RAND_MAX;
}
i T(v o, v d, f&t, v&n)
{
t = 1e9; i m = 0;
f p = -o.z / d.z;
if(.01 < p)t = p, n = v(0, 0, 1), m = 1;
for(i k = 19; k--;)
for(i j = 9; j--;)if(G[j] & 1 << k) {
v p = o + v(-k, 0, -j - 4);
f b = p % d, c = p % p - 1, q = b * b - c;
if(q > 0) {
f s = -b - sqrt(q);
if(s < t && s > .01)
t = s, n = !(p + d * t), m = 2;
}
}
return m;
} v S(v o, v d)
{
f t;
v n;
i m = T(o, d, t, n);
if(!m)return v(.7, .6, 1) * pow(1 - d.z, 4);
v h = o + d * t, l = !(v(9 + R(), 9 + R(), 16) + h * -1), r = d + n * (n % d * -2);
f b = l % n; if(b < 0 || T(h, l, t, n))b = 0;
f p = pow(l % r * (b > 0), 99);
if(m & 1) {
h = h * .2;
return((i)(ceil(h.x) + ceil(h.y)) & 1 ? v(3, 1, 1) : v(3, 3, 3)) * (b * .2 + .1);
} return v(p, p, p) + S(h, r) * .5;
} i
main()
{
FILE * pFile;
pFile = fopen("d:\\myfile3.ppm", "w");
fprintf(pFile,"P6 512 512 255 ");
v g = !v(-6, -16, 0), a = !(v(0, 0, 1) ^ g) * .002, b = !(g ^ a) * .002, c = (a + b) * -256 + g;
for(i y = 512; y--;)
for(i x = 512; x--;) {
v p(13, 13, 13);
for(i r = 64; r--;) {
v t = a * (R() - .5) * 99 + b * (R() - .5) * 99;
p = S(v(17, 16, 8) + t, !(t * -1 + (a * (R() + x) + b * (y + R()) + c) * 16)) * 3.5 + p;
}
fprintf(pFile, "%c%c%c", (i)p.x, (i)p.y, (i)p.z);
}
}
My dear friend that's Paul Heckbert code's right?
You could at least mention it.
For people thinking this code is unreadable, here is why:
This guy made a code that could fit on a credit card, that was the goal :)
His website: http://www.cs.cmu.edu/~ph/
Edit: Knowing the source of this code may help you understand it. Even if it'snot your main motivation...
If you are really interested in raytracing, start with other sources.
Take a look at this website http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-1-writing-a-simple-raytracer/source-code/ (Plus it talk about your code)
This code is not really special. It is basically a ray tracer that was obfuscated into a form that makes it fit on a business card (see https://www.cs.cmu.edu/~ph/).
How is that actually done without any modelling tools?
You don't need tools to render anything. You could even create a complete game of WoW (or what's hip at the moment) without any modelling tool. Modelling tools just make your live easier w.r.t. certain kinds of scenes (read: very complex ones).
You could always hardcode these data, or hack them manually into some external file.
You could also use parametric generators; Perlin Noise is one of the more popular examples thereof.
In a ray tracer, it happens that it is very simple to start out without modelling tools, as it is very simple to calculate geometric intersections between the rendering primitive "ray" and any finite geometric primitive. E.g., intersection a non-approximated, "perfect" sphere is just a few lines of code.
tl;dr: Data is just data. How you create and crunch it is completely up to you.

Mathematica parametric equations and list plot

x = e^1/2*Cos((pi/4)t) + Cos(2Pi*t)
y = e^1/3*1/2Sin(pi*t) + sin(2pi * t)
t E [a,b] where 'a' and 'b' are inputted via input. How can I draw where 't' is in the moments t = a, a+1, a+2, ..., b.
I am not sure I understand what you mean by How can I draw where t is'. But if you want to drawParametricPlotint` the command is
x = Exp[1/2]*Cos[(Pi/4) t] + Cos[2 Pi*t] ;
y = Exp[1/3]*1/2 Sin[Pi*t] + Sin[2 Pi*t];
a = -1; b = 1;
ParametricPlot[{x, y}, {t, a, b}]

How to convert NFA to Regular Expression

I knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D
Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system 𝐪 ≥ 𝐚 + H 𝐪, where 𝐪 is the vector of all the variables, 𝐚 the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to 𝐪 ≥ 𝐚 + H 𝐪, in matrix form is 𝐪 = H* 𝐚.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system 𝐪 ≥ 𝐚 + H 𝐪.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
a: q → q, b: q → r, c: q → s,
d: r → q, e: r → r, f: r → s,
g: s → q, h: s → r, i: s → s.
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
q ≥ x + a q + b r + c s,
r ≥ y + d q + e r + f s,
s ≥ z + g q + h r + i s.
Solve for s, first, to obtain
s = i* (z + g q + h r) = i* z + i* g q + i* h r.
Substitute in the other inequalities to get:
q ≥ x + c i* z + (a + c i* g) q + (b + c i* h) r,
r ≥ y + f i* z + (d + f i* g) q + (e + f i* h) r.
Rewrite this as
q ≥ x' + a' q + b' r,
r ≥ y' + d' q + e' r,
where
x' = x + c i* z, a' = a + c i* g, b' = b + c i* h,
y' = y + f i* z, d' = d + f i* g, e' = e + f i* h.
Solve for r to get
r = e'* (y' + d' q) = e'* y' + e'* d' q.
Substitute into the inequality for q to get
q ≥ (x' + b' e'* y') + (a' + b e'* d') q
and rewrite this as
q ≥ x" + a" q
where
x" = x' + b' e'* y', a" = a' + b e'* d'.
Finally, solve this for q to get
q = a"* x".
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
a: q → r, a: q → q, b: q → q, b: q → s, a: s → s, b: s → s
The corresponding right-affine system is
q ≥ a r + a q + b q
r ≥ b s
s ≥ 1 + a s + b s
Solve for s first:
s ≥ 1 + a s + b s = 1 + (a + b) s ⇒ s = (a + b)*.
Substitute into the inequality for r and solve to find the least fixed point:
r ≥ b (a + b)* ⇒ r = b (a + b)*.
Finally, substitute into the inequality for q and solve to find the least fixed point:
q ≥ a b (a + b)* + (a + b) q ⇒ q = (a + b)* a b (a + b)*.
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.