Wrong Wolfram Mathematica code translation - c++

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]

Related

how do I tell sympy that a variable is known?

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)]

Fsolve equivalent in C++

I am trying to replicate Matlab's Fsolve as my project is in C++ solving an implicit RK4 scheme. I am using the NLopt library using the NLOPT_LD_MMA algorithm. I have run the required section in matlab and it is considerably faster. I was wondering whether anyone had any ideas of a better Fsolve equivalent in C++? Another reason is that I would like f1 and f2 to both tend to zero and it seems suboptimal to calculate the L2 norm to include both of them as NLopt seems to only allow a scalar return value from the objective function. Does anyone have any ideas of an alternative library or perhaps using a different algorithm/constraints to more closely replicate the default fsolve.
Would it be better (faster) perhaps to call the python scipy.minimise.fsolve from C++?
double implicitRK4(double time, double V, double dt, double I, double O, double C, double R){
const int number_of_parameters = 2;
double lb[number_of_parameters];
double ub[number_of_parameters];
lb[0] = -999; // k1 lb
lb[1] = -999;// k2 lb
ub[0] = 999; // k1 ub
ub[1] = 999; // k2 ub
double k [number_of_parameters];
k[0] = 0.01;
k[1] = 0.01;
kOptData addData(time,V,dt,I,O,C,R);
nlopt_opt opt; //NLOPT_LN_MMA NLOPT_LN_COBYLA
opt = nlopt_create(NLOPT_LD_MMA, number_of_parameters);
nlopt_set_lower_bounds(opt, lb);
nlopt_set_upper_bounds(opt, ub);
nlopt_result nlopt_remove_inequality_constraints(nlopt_opt opt);
// nlopt_result nlopt_remove_equality_constraints(nlopt_opt opt);
nlopt_set_min_objective(opt,solveKs,&addData);
double minf;
if (nlopt_optimize(opt, k, &minf) < 0) {
printf("nlopt failed!\n");
}
else {
printf("found minimum at f(%g,%g,%g) = %0.10g\n", k[0],k[1],minf);
}
nlopt_destroy(opt);
return V + (1/2)*dt*k[0] + (1/2)*dt*k[1];```
double solveKs(unsigned n, const double *x, double *grad, void *my_func_data){
kOptData *unpackdata = (kOptData*) my_func_data;
double t1,y1,t2,y2;
double f1,f2;
t1 = unpackdata->time + ((1/2)-(1/6)*sqrt(3));
y1 = unpackdata->V + (1/4)*unpackdata->dt*x[0] + ((1/4)-(1/6)*sqrt(3))*unpackdata->dt*x[1];
t2 = unpackdata->time + ((1/2)+(1/6)*sqrt(3));
y2 = unpackdata->V + ((1/4)+(1/6)*sqrt(3))*unpackdata->dt*x[0] + (1/4)*unpackdata->dt*x[1];
f1 = x[0] - stateDeriv_implicit(t1,y1,unpackdata->dt,unpackdata->I,unpackdata->O,unpackdata->C,unpackdata->R);
f2 = x[1] - stateDeriv_implicit(t2,y2,unpackdata->dt,unpackdata->I,unpackdata->O,unpackdata->C,unpackdata->R);
return sqrt(pow(f1,2) + pow(f2,2));
My matlab version below seems to be a lot simpler but I would prefer the whole code in c++!
k1 = 0.01;
k2 = 0.01;
x0 = [k1,k2];
fun = #(x)solveKs(x,t,z,h,I,OCV1,Cap,Rct,static);
options = optimoptions('fsolve','Display','none');
k = fsolve(fun,x0,options);
% Calculate the next state vector from the previous one using RungeKutta
% update equation
znext = z + (1/2)*h*k(1) + (1/2)*h*k(2);``
function [F] = solveKs(x,t,z,h,I,O,C,R,static)
t1 = t + ((1/2)-(1/6)*sqrt(3));
y1 = z + (1/4)*h*x(1) + ((1/4)-(1/6)*sqrt(3))*h *x(2);
t2 = t + ((1/2)+(1/6)*sqrt(3));
y2 = z + ((1/4)+(1/6)*sqrt(3))*h*x(1) + (1/4)*h*x(2);
F(1) = x(1) - stateDeriv_implicit(t1,y1,h,I,O,C,R,static);
F(2) = x(2) - stateDeriv_implicit(t2,y2,h,I,O,C,R,static);
end

Merge two Mat-Objects in OpenCV

I wrote a little function in order to be able to "stick" the pixels of an image on top of another, but it somehow doesnt work: While the "shapes" of my "sticked" image is right, the colors are not.
The example flower is the first image, and as a second image I had a black trapezoid png. As you can see, there are multiple problems:
1. The colors are shown weirdly. Actually there are no colors, just greyscale, and some weird stripes as overlay.
2. Alpha values are not respected. The white part of the overlay image is transparent in the png.
Here is my code:
void mergeMats(Mat mat1, Mat mat2, int x, int y){
//unsigned char * pixelPtr = (unsigned char *)mat2.data;
//unsigned char * pixelPtr2 = (unsigned char *)mat1.data;
//int cn = mat2.channels();
//int cn2 = mat2.channels();
//Scalar_<unsigned char> bgrPixel;
for (int i = 0; i < mat2.cols; i++){
for (int j = 0; j < mat2.rows; j++){
if (x + i < mat1.cols && y + j < mat1.rows){
Vec3b &intensity = mat1.at<Vec3b>(j+y, i+x);
Vec3b intensity2 = mat2.at<Vec3b>(j, i);
for (int k = 0; k < mat1.channels(); k++) {
intensity.val[k] = saturate_cast<uchar>(intensity2.val[k]);
}
//pixelPtr2[(i + x)*mat1.cols*cn2 + (j + y)*cn2 + 0] = pixelPtr[(i + x)*mat2.cols*cn + (j + y)*cn + 0];
//pixelPtr2[(i + x)*mat1.cols*cn2 + (j + y)*cn2 + 1] = pixelPtr[(i + x)*mat2.cols*cn + (j + y)*cn + 1];
//pixelPtr2[(i + x)*mat1.cols*cn2 + (j + y)*cn2 + 2] = pixelPtr[(i + x)*mat2.cols*cn + (j + y)*cn + 2];
}
}
}
}
The commented code was another approach, but had the same results.
So, here are my questions:
1. How do I solve the 2 problems (1. the colors..., 2. alpha ...)
2. How is the pixel-array of any Mat-Object actually, well, organized? I guess it would be easier for me to manipulate those arrays if I knew whats in them.
Because you are iterating mat2 with wrong type. Change Vec3b intensity2 = mat2.at<Vec3b>(j, i); to:
Vec4b intensity2 = mat2.at<Vec4b>(j, i);
and the weird stripes are eliminated. And use intensity2[3] to deal with the alpha channel.
Assume that you are reading the black trapezoid png file using -1 flag:
auto trapezoidImg = cv::imread("trapezoid.png", -1);
where the -1 flag specifies that alpha channel is read. Then trapezoidImg is organized in the following format:
[B, G, R, A, B, G, R, A, ......;
B, G, R, A, B, G, R, A, ......;
......
B, G, R, A, B, G, R, A, ......]
You can print out trapezoidImg, for example using std::cout, to figure out this format.
If you read trapezoidImg using at<Vec3b>, what you get in fact is (B, G, R), (A, B, G), (R, A, B), ......, and this is where the weird stripes came from. Therefore, use at<Vec4b> to read the (R, G, B, A) intensity correctly.
Next, you should define what to do with the alpha channel. You can mixing two Mat or override another, whatever. One simple method is to override mat1 only when the alpha channel in mat2 is large enough:
cv::Vec3b &intensity = mat1.at<cv::Vec3b>(j + y, i + x);
cv::Vec4b intensity2 = mat2.at<cv::Vec4b>(j, i);
for (int k = 0; k < mat1.channels(); k++) {
if (intensity2.val[3] > 250){ //3 for alpha channel
intensity.val[k] = cv::saturate_cast<uchar>(intensity2.val[k]);
}
}
This is enough to deal with your black trapezoid png with transparent background. Or further extend the rule by mixing two Mat:
cv::Vec3b &intensity = mat1.at<cv::Vec3b>(j + y, i + x);
cv::Vec4b intensity2 = mat2.at<cv::Vec4b>(j, i);
auto alphaValue = cv::saturate_cast<uchar>(intensity2.val[3]);
auto alpha = alphaValue / 255.0;
for (int k = 0; k < 2; k++) { //BGR channels only
intensity.val[k] = cv::saturate_cast<uchar>(
intensity2.val[k] * alpha + intensity.val[k] * (1.0 - alpha));
}

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}]