Translate 2-D PDEs and conditions into accurate Fipy codes - 2d

enter image description here
The image includes the control equation,initial and boundary condition.It descripe a heat transfer problem between a plate and fluid.
I don't know how to use fipy to encode the 2-d problem and boundary condition that include the var.
Here is my attempt.
from fipy import *
import numpy as np
#constant
Pe=2400
le_L=1/20000
L_l=20000
alphas=1
alphaf=1
a=1/Pe+le_L
b=1/Pe+L_l
Bi=0.4
c=Bi/Pe*L_l
#generate
mesh=Grid2D(dx=1,dy=1)
Ts=CellVariable(mesh=mesh,name='Ts',value=900)
Tf=CellVariable(mesh=mesh,name='Tf',value=300)
#condition
Ts.faceGrad.constrain([0.],mesh.facesLeft)
Ts.faceGrad.constrain([0.],mesh.facesRight)
Ts.faceGrad.constrain([-1.*Bi*(Tf.value-Ts.value)],mesh.facesBottom)
Ts.faceGrad.constrain([0.],mesh.facesTop)
Tf.constrain(300,mesh.facesLeft)
Tf.grad.constrain(0,mesh.facesRight)
a=CellVariable(mesh=mesh,rank=1)
a[:]=1
#eq
eq1=TransientTerm(var=Ts)==DiffusionTerm(coeff=[[a,b]],var=Ts)
eq2=TransientTerm(var=Tf)==DiffusionTerm(coeff=[[a,0]],var=Tf)-
ExponentialConvectionTerm(a,var=Tf)+ImplicitSourceTerm(c,var=Tf)-
ImplicitSourceTerm(c,var=Ts)
eq=eq1&eq2
#solve
dt=0.1
steps=100
viewer=Viewer(vars=(Ts,Tf),datamax=1000,datamin=0)
for i in range(steps):
eq.solve(dt=dt)
viewer.plot()
I find it failed.And I don't know where goes wrong.I would welcome any help; many thanks!
BTW,the final image i wish to get is likeenter image description here
......Many thx!

[edited to fix general boundary conditions]
The following runs and seems to give results of the nature you're looking for:
from fipy import *
import numpy as np
#constant
Pe=2400.
le_L=1./20000.
L_l=20000.
alphasx=alphasy=1.
alphaf=1.
Bi=0.4
c=Bi/Pe*L_l
Dsxx = alphasx
Dsyy = alphasy * L_l**2
Ds = 1./Pe * le_L * (1./alphaf) * Variable([[alphasx, 0.],
[0., alphasy * L_l**2]])
Df = Variable([[1./Pe * le_L, 0],
[0., 0.]])
#generate
mesh=Grid2D(Lx=1.,Ly=1.,nx=100, ny=100)
Ts=CellVariable(mesh=mesh,name='Ts',value=900.)
Tf=CellVariable(mesh=mesh,name='Tf',value=900.)
#condition
bottom_mask = (mesh.facesBottom * mesh.faceNormals).divergence
dPR = mesh._cellDistances[mesh.facesBottom.value][0]
Af = mesh._faceAreas[mesh.facesBottom.value][0]
bottom_coeff = bottom_mask * Ds[1,1] * Af / (1 + dPR)
Tf.constrain(300,mesh.facesLeft)
#eq
eq1=(TransientTerm(var=Ts)==DiffusionTerm(coeff=Ds,var=Ts)
+ ImplicitSourceTerm(coeff=bottom_coeff * -Bi, var=Tf)
- ImplicitSourceTerm(coeff=bottom_coeff * -Bi, var=Ts))
eq2=(TransientTerm(var=Tf)==DiffusionTerm(coeff=Df,var=Tf)
-ExponentialConvectionTerm(coeff=[[1.], [0]],var=Tf)
+ImplicitSourceTerm(c,var=Tf)
-ImplicitSourceTerm(c,var=Ts))
eq=eq1&eq2
#solve
dt=0.01
steps=100
viewer=Viewer(vars=(Ts,Tf),datamax=1000,datamin=0)
for i in range(steps):
eq.solve(dt=dt)
viewer.plot()
I changed a number of coefficients to agree with the mathematics you provided.
I fixed the diffusion coefficients to have the shape expected by FiPy for anistropic diffusion
I changed lots of ints to floats because ints don't work well in FiPy
I provided a domain to solve over (your mesh only had a single cell in it, making spatial variation impossible)
I decreased the time step
I introduced the best way we know how to deal with general boundary conditions. It's ugly, but I think it's right.
You will probably also want to introduce sweeping to account for the nonlinear dependency between the equations and the boundary conditions.

Related

3D bounding box for an item with three axis rotations

I'm trying to find the <x,y,z> size of what would end up being a bounding box for a rotated shape in all three axis rotations. Though to help keep it simple, the example demonstrated below has only the x axis rotated.
vector Size = <10,1,0.5>; vector Deg = <22.5,0,0>
if(Deg.x > 0 && Deg.y == 0 && Deg.z == 0){
Y1 = Cos(Deg.x) * Size.y + Sin(Deg.x) * Size.z;
Z1 = Cos(Deg.x) * Size.z + Sin(Deg.x) * Size.y;}
Below are for the y and z rotations, that is if you decided to change the degrees to say <0,22.5,0> and <0,0,22.5>.
if(Deg.y > 0 && Deg.x == 0 && Deg.z == 0){
X2 = Cos(Deg.y) * Size.x + Sin(Deg.y) * Size.z;
Z2 = Cos(Deg.y) * Size.z + Sin(Deg.y) * Size.x;}
if(Deg.z > 0 && Deg.x == 0 && Deg.y == 0){
X3 = Cos(Deg.z) * Size.x + Sin(Deg.z) * Size.y;
Y3 = Cos(Deg.z) * Size.y + Sin(Deg.z) * Size.x;}
Though the part I get lost at is, where do I go from here if when you have the rotation in two or three axis. Such as <22.5,22.5,0> or <22.5,22.5.22.5>
Is there a website with a tutorial or example equations I could review or are there any hints or ideas of what I could do to figure this out.
EDIT:
I do want to add that the comment from Nico helped, in that what I'm asking about is called: Axis-Aligned Bounding Box or AABB for short.
As for JohanC comment about the 22.5, yes the Deg = Degree. Also yes you'll have to turn the Degree into Radians, but I put it as Degree in the example for the Sin and Cos input to keep it simple.
If you're wondering how this question might be useful. Well as an example you'd need to know the AABB to help in an equation to keep the item in question flush with the surface its sitting on if you were to say resize the item while it had <x,y,z> rotations that weren't at perfect zero rotations.
I found my own solution after a lot of testing and debugging. I'll show and explain a small portion of the script I created since functions and options from coding language to language may vary.
A few details about the example script below. Deg = Degrees. Though yes what JohanC mentioned about radians is correct, since even the language I'm using I have to convert degrees to radians with a function. Not every language or calculator is like that, yet for intents and purposes to make it easier to read, I did take out the excess while keeping the meat of the code/equation intact so to speak. Also in this example, I'm rotating it in the Z axis to demonstrate and as a starting point.
vector Size = <10,1,0.5>; //Insert your own coding language function
//for splitting the size in half with positive and negative halves.
vector min = <-5,-0.5,-0.25>; vector max = <5,0.5,0.25>;
//You'll need at least 8 points in total to do this correctly.
//I already have 8 points added down below, labeled p1 - p8
vector p1 = <max.x,max.y,max.z>; vector p2 = <max.x,min.y,max.z>;
vector p3 = <max.x,max.y,min.z>; vector p4 = <max.x,min.y,min.z>;
vector p5 = <min.x,max.y,max.z>; vector p6 = <min.x,min.y,max.z>;
vector p7 = <min.x,max.y,min.z>; vector p8 = <min.x,min.y,min.z>;
vector Deg = <0,0,22.5>
//This will give you an idea of how to setup the x,y,z for each point
//equation to make / change to fit the rotations. Plus to make it compact
//I suggest that you use list, a while loop, and etc.
x1 = p1.x * llCos(Deg.z) - p1.y * llSin(Deg.z);
y1 = p1.y * llCos(Deg.z) + p1.x * llSin(Deg.z);
A lot of what I kept out of the example above is several list, true and false statements, a while loop, and few other things. Though it's kept simple to show the less complicated portion of it and the fact that not every language has the same functions available.
Though once when you get all of the location data of the 8 points collected, you can then put each of the x, y, z point information into its own list. Then run the equation (edited as needed for each rotation). After that, get the max and min from each axis rotation output. Then take the maximum and minus the minimum from it since the minimum will always be a negative. That right there will give you the <x,y,z> size of your bounding box.
I do want to add that the link from Nico helped slightly, though the only flaw in it is "if" the language you're using allows a matrix or "if" you're able to create a multidimensional array. If you can manage that, then use the link Nico gave to see if it helps.
Also some of JohanC tips/hints helped as well. Speaking of which, the part about "using the output of the vector from one rotation to the next" works well if and only if you're using the 8 points method. Otherwise if you try it with the bounding box size of one to the next, the first rotation to the second will work fine because its only moving in 2D at that point, but by the time you try to make the third rotation with the second rotations bounding box it won't work because it'll be going from 2D to 3D.
Note - If you think you have a better way to do the equation or simpler way to explain it, feel free to add in your own answer.

What's a simple way of warping an image with a given set of points?

I'd like to implement image morphing, for which I need to be able to deform the image with given set of points and their destination positions (where they will be "dragged"). I am looking for a simple and easy solution that gets the job done, it doesn't have to look great or be extremely fast.
This is an example what I need:
Let's say I have an image and a set of only one deforming point [0.5,0.5] which will have its destination at [0.6,0.5] (or we can say its movement vector is [0.1,0.0]). This means I want to move the very center pixel of the image by 0.1 to the right. Neighboring pixels in some given radius r need to of course be "dragged along" a little with this pixel.
My idea was to do it like this:
I'll make a function mapping the source image positions to destination positions depending on the deformation point set provided.
I will then have to find the inverse function of this function, because I have to perform the transformation by going through destination pixels and seeing "where the point had to come from to come to this position".
My function from step 1 looked like this:
p2 = p1 + ( 1 / ( (distance(p1,p0) / r)^2 + 1 ) ) * s
where
p0 ([x,y] vector) is the deformation point position.
p1 ([x,y] vector) is any given point in the source image.
p2 ([x,y] vector) is the position, to where p1 will be moved.
s ([x,y] vector) is movement vector of deformation point and says in which direction and how far p0 will be dragged.
r (scalar) is the radius, just some number.
I have problem with step number 2. The calculation of the inverse function seems a little too complex to me and so I wonder:
If there is an easy solution for finding the inverse function, or
if there is a better function for which finding the inverse function is simple, or
if there is an entirely different way of doing all this that is simple?
Here's the solution in Python - I did what Yves Daoust recommended and simply tried to use the forward function as the inverse function (switching the source and destination). I also altered the function slightly, changing exponents and other values produces different results. Here's the code:
from PIL import Image
import math
def vector_length(vector):
return math.sqrt(vector[0] ** 2 + vector[1] ** 2)
def points_distance(point1, point2):
return vector_length((point1[0] - point2[0],point1[1] - point2[1]))
def clamp(value, minimum, maximum):
return max(min(value,maximum),minimum)
## Warps an image accoording to given points and shift vectors.
#
# #param image input image
# #param points list of (x, y, dx, dy) tuples
# #return warped image
def warp(image, points):
result = img = Image.new("RGB",image.size,"black")
image_pixels = image.load()
result_pixels = result.load()
for y in range(image.size[1]):
for x in range(image.size[0]):
offset = [0,0]
for point in points:
point_position = (point[0] + point[2],point[1] + point[3])
shift_vector = (point[2],point[3])
helper = 1.0 / (3 * (points_distance((x,y),point_position) / vector_length(shift_vector)) ** 4 + 1)
offset[0] -= helper * shift_vector[0]
offset[1] -= helper * shift_vector[1]
coords = (clamp(x + int(offset[0]),0,image.size[0] - 1),clamp(y + int(offset[1]),0,image.size[1] - 1))
result_pixels[x,y] = image_pixels[coords[0],coords[1]]
return result
image = Image.open("test.png")
image = warp(image,[(210,296,100,0), (101,97,-30,-10), (77,473,50,-100)])
image.save("output.png","PNG")
You don't need to construct the direct function and invert it. Directly compute the inverse function, by swapping the roles of the source and destination points.
You need some form of bivariate interpolation, have a look at radial basis function interpolation. It requires to solve a linear system of equations.
Inverse distance weighting (similar to your proposal) is the easiest to implement but I am afraid it will give disappointing results.
https://en.wikipedia.org/wiki/Multivariate_interpolation#Irregular_grid_.28scattered_data.29

Arrange X amount of things evenly around a point in 3d space

If I have X amount of things (lets just randomly say 300)
Is there an algorithm that will arrange these things somewhat evenly around a central point? Like a 100 sided dice or a 3d mesh of a sphere?
Id rather have the things somewhat evenly spaced like this..
Rather than this polar way..
ps. For those interested, wondering why do I want to do this?
Well I'm doing these for fun, and after completing #7 I decided I'd like to represent the array of wires in 3d in Unity and watch them operate in a slowed down manner.
Here is a simple transformation that maps a uniform sample in the rectangle [0, 2 pi] x [-1, 1] onto a uniform sample on the sphere of radius r:
T(phi, z) = (r cos(phi) sqrt(1 - z^2), r sin(phi) sqrt(1 - zˆ2), r z)
The reason why this transformation produces uniform samples on the sphere is that the area of any region T(U) obtained by transforming the region U from the rectangle does not depend on U but on the area of U.
To prove this mathematically it is enough to verify that the norm of the vectorial product
| ∂T/∂phi x ∂T/∂z |
is constant (the area on the sphere is the integral of this vectorial product w.r.t. phi and z).
Summarizing
To produce a random sample uniformly distributed in the Sphere of radius r do the following:
Produce a random sample (phi_1, ..., phi_n) uniformly distributed in [0, 2 pi].
Produce a random sample (z_1, ..., z_n) uniformly distributed in [-1, 1].
For every pair (phi_j, z_k) calculate T(phi_j, z_k) using the formula above.
Here's a three-step approach. 1a) Make more points than you need. 1b) Remove some. 2) Adjust the rest.
1a) To make more points that you need, take any quasiregular polyhedron with sides that tessellate (triangles, squares, diamonds). Tesselate the spherical faces by subdivision, generating more vertices. For example, if you use the regular icosahedron you get geodesic domes. (Subdivide by 2, you get the dual to the C60 buckyball.) Working out exact formulas isn't hard. The number of new vertices per face is quadratic in the subdivision.
1b) Randomly remove enough points to get you down to your target number.
2) Use a force-directed layout algorithm to redistribute the vertices over the sphere. The underlying force graph is just that provided by the nearest neighbors in your underlying tesselation.
There are other ways to do step 1), such as just generating random points in any distribution. There is an advantage of starting with a quasiregular figure, though. Force-directed algorithms have a reputation for poor convergence in some cases. By starting with something that's already mostly optimal, you'll bypass most all of any convergence problems you might have.
One elegant solution I came across recently is a spherical fibonacci lattice (http://extremelearning.com.au/how-to-evenly-distribute-points-on-a-sphere-more-effectively-than-the-canonical-fibonacci-lattice/)
The nice thing about it is that you can specify the exact number of points you want
// C# Code example
Vector3[] SphericalFibonacciLattice(int n) {
Vector3[] res = new Vector3[n];
float goldenRatio = (1.0f + MathF.Sqrt(5.0f)) * 0.5f;
for(int i = 0; i < n; i++)
{
float theta = 2.0f * MathF.PI * i / goldenRatio;
float phi = MathF.Acos(1.0f - 2.0f * (i + 0.5f) / n);
Vector3 p = new Vector3(MathF.Cos(theta) * MathF.Sin(phi),
MathF.Sin(theta) * MathF.Sin(phi),
MathF.Cos(phi));
res[i] = p;
}
return res;
}
The linked article extends on this to create an even more uniform distribution, but even this basic version creates very nice results.

The time-corrected Verlet numerical integration formula

There is a commonly used verlet-integration formula on the web by Johnathan Dummer, called Time-Corrected Verlet. However I've read several forum posts, that people get weird or unexpected results with it in certain conditions.
Formula by Johnathan Dummer:
x1 = x + (x – x0) * dt / dt0 + a * dt^2
There is also a stackoverflow answer, which states that Dummer's time-corrected formula is broken and the poster presents his own derivation as the correct one.
Suggested correct formula by a stackoverflow answer
x1 = x + (x – x0) * dt / dt0 + a * dt * (dt + dt0) / 2
Well, is Dummer's formula really broken? If yes, is the derivation of the poster better?
PS: It is also weird that Dummer uses the verlet integration formula x1 = x - x0 + a * dt^2 on his website instead of the correct x1 = 2x - x0 + a * dt^2.
The wikipedia page Verlet integration - Non-constant time differences presents the two formula, without referenced. I've not checked the derivation myself but the reasoning for the second improved formula looks sound.
I've downloaded Dummer's spreadsheet and modified one of the formula to use the correction. The results are much better.
The exact results are in yellow, we see that just using the normal Verlet algorithm with fluctuating frame-rate is bad. Dummer's time-correct varient in red is pretty good, but a little off. The dark green version with the improved correction is much better.
For projectiles under gravity which has a quadratic solution you may find that the improved version is exact. When the degree gets a bit higher it will vary from the true path and it might be worth testing to see if we still get a better approximation.
Doing the same calculation for a sin curve shows the improved method is considerably better. Here Time-correct Verlet is drifting quite a bit. The improved version is better, only a little bit off the exact answer.
For the PS. Note that if you set dt=dt0 in the TCV formula
x1 = x + (x – x0) * dt / dt0 + a * dt^2
you get
x1 = x + x – x0 + a * dt^2
= 2 x – x0 + a * dt^2
the original Verlet formula.
The true derivation is based on Taylor formulas
x(t-h0) = x(t) - x'(t)*h0 + 0.5*x''(t)*h0^2 + O(h0^3)
x(t+h1) = x(t) + x'(t)*h1 + 0.5*x''(t)*h1^2 + O(h1^3)
and now eliminate x'(t) from these two formulas to get a Verlet-like formula
h0*x(t+h1) + h1*x(t-h0) = (h0+h1)*x(t) + 0.5*a(t)*h0*h1*(h0+h1) +O(h^3)
which makes a propagation formula
x(t+h1) = (1+h1/h0)*x(t) - h1/h0*x(t-h0) + 0.5*a(t)*h1*(h0+h1)
= x(t) + (x(t)-x(t-h0))*h1/h0 + 0.5*a(t)*h1*(h0+h1)
so that indeed the corrected formula is the correct one.
Note that if you use velocity Verlet steps
Verlet(dt) {
v += a * 0.5*dt
x += v*dt
a = acceleration(x)
v += a * 0.5*dt
}
then each step is indepentently symplectic, so that the change of step size between steps is absolutely unproblematic.
Notice that the main advantages of Verlet and other similar symplectic schemes over Runge-Kutta methods etc. crucially depends on using a fixed step. In detail, the modified energy function (and other more than quadratic constants of motion) that is largely constant under the numerical method is a modification of the exact energy where the difference scales with the step size. So when changing the step size a different modification gives a constant energy. Frequent changes of the step size allow thus, possibly, arbitrary changes of the energy level.
I decided to quit being lazy and show some kind of derivation of how the original Verlet method looks with a variable step size. Because it seems like this faulty adaptation by Dummer is more pervasive than I thought, which is saddening. I also noticed that, as the above answer points out, the correct version is now on wikipedia alongside the Dummer one, though it was added after my "suggested correct answer".
When I look at Verlet method, I see that it looks a lot like leapfrog, velocity Verlet, implicit Euler etc., which look like second-order versions of modified midpoint, and some of them may be identical. In each of these, to some degree they have a leapfrog idea where integration of acceleration (into velocity) and integration of constant velocity (into position) are each staggered so that they overlap by half. This brings things like time-reversibility and stability, which are more important for the 'realism' of a simulation than accuracy is. And the 'realism', the believability, is more important for video games. We don't care if something moves to a slightly different position than it's exact mass would have truly caused, so long as it looks and feels realistic. We're not calculating where to point our high-powered satellite telescopes to look at features on distant objects, or future celestial events. Here, stability and efficiency takes priority here over mathematical accuracy. So, it seems like leapfrog method is appropriate. When you adapt leapfrog for variable time step, it loses some of this advantage, and it loses some of it's appeal for game physics. Stormer-Verlet is like leapfrog, except it uses the average velocity of the previous step instead of a separately maintained velocity. You can adapt this Stormer-Verlet in the same way as leapfrog. To integrate velocity forward with a fixed acceleration, you use half the length of the previous step and half the length of the next step, because they are staggered. If the steps were fixed like true leapfrog, they would be the same length and so the two half lengths sum to one. I use h for step size, a/v/p for acceleration/velocity/position, and hl/pl for 'last' as in previous step. These aren't really equations, more like assignment operations.
Original leapfrog:
v = v + a*h
p = p + v*h
With variable time step:
v = v + a*hl/2 + a*h/2
p = p + v*h
Factor a/2:
v = v + a*(hl + h)/2
p = p + v*h
Use previous position (p - pl)/hl for initial velocity:
v = (p - pl)/hl + a*(hl + h)/2
p = p + v*h
Substitute, we don't need v:
p = p + ( (p - pl)/hl + a*(hl + h)/2)*h
Distribute h:
p = p + (p - pl)*h/hl + a*h*(h + hl)/2
The result is not as simple or fast as the original Stormer form of Verlet, 2p - pl + a*h^2. I hope this makes some sense. You would omit the last step in actual code, no need to multiply h twice.

Matrix transforms; concepts and theory, are there any free resources for learning practically? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I've been having fun rendering charts and graphs from co-ordinates lately, and I'm fascinated by using matrices to transform co-ordinate spaces.
I've been able to successfully scale and invert 2 dimensional co-ordinate spaces, but now my appetite is whetted. :)
Where can I go for clear, informative, (free), educational material on matrices, matrix math, especially as applies to 2 and 3 dimensional space?
Original answer: I'm not sure if you will like how mathematical courses typically introduce matrices. As a programmer, you might be happier with grabbing any decent 3D graphics book. It should certainly have very concrete 3x3 matrices. Also, find out the ones that will teach you projective transformations (projective geometry is a very beautiful area of low-dimensional geometry and easy to program).
Mini-course in matrix math with Python 3
Contents:
Matrices [Vector, __add__, reflect_y, rotate, dilate, transform]
Matrices: Overloaded [Matrix, __add__, __str__, __mul__, zero, det, inv, __pow__]
Bonus: Complex numbers
Matrices: The (R)evolution. It's already in the making (there's a summary at the end)
Preface: Based on my teaching experience, I think that the courses referenced by others are very good courses. That means if your goal is understanding matrices as mathematicians do then you should by all means get the whole course. But if your goals are more modest, here's my try at something more tailored to your needs (but still written with the goal to convey many theoretical concepts, kind of contradicting my original advice.)
How to use:
This post is long. You might consider printing this and going slow, like one part a day.
Code is essential. This is a course for programmers. Exercises are essential too.
You should take a look at the code companion which contains all this code and much more
It's "2 for the price of 1" special: you can also learn Python 3 here. And complex numbers.
I'll highly value any attempt to read this (do I officially qualify for the longest post ever?), so feel free to comment if you don't understand something (and also if you do).
Matrices
=
Vectors
Before matrices come vectors. You sure know how to handle the 2- and 3-dimensional vectors:
class Vector:
"""This will be a simple 2-dimensional vector.
In case you never encountered Python before, this string is a
comment I can put on the definition of the class or any function.
It's just one of many cool features of Python, so learn it here!
"""
def __init__(self, x, y):
self.x = x
self.y = y
now you can write
v = Vector(5, 3)
w = Vector(7, -1)
but it's not much fun by itself. Let's add more useful methods:
def __str__(self: 'vector') -> 'readable form of vector':
return '({0}, {1})'.format(self.x, self.y)
def __add__(self:'vector', v: 'another vector') -> 'their sum':
return Vector(self.x + v.x, self.y + v.y)
def __mul__(self:'vector', number: 'a real number') -> 'vector':
'''Multiplies the vector by a number'''
return Vector(self.x * number, self.y * number)
That makes things more interesting as we can now write:
print(v + w * 2)
and get the answer (19, 1) nicely printed as a vector (if the examples look unfamiliar, think how this code would look in C++).
Transformations
Now it's all cool to be able to write 1274 * w but you need more vector operations for the graphics. Here are some of them: you can flip the vector around the (0,0) point, you can reflect it around x or y axis, you can rotate it clockwise or counterclockwise (it's a good idea to draw a picture here).
Let's do some simple operations:
...
def flip(self:'vector') -> 'vector flipped around 0':
return Vector(-self.x, -self.y)
def reflect_x(self:'vector') -> 'vector reflected around x axis':
return Vector(self.x, -self.y)
print(v.flip(), v.reflect_x())
Question: is it possible to express flip(...) using the operations I had below? What about reflect_x?
Now you may wonder why I omitted reflect_y. Well, it's because I want you to stop for a moment and write your own version of it. Ok, here's mine:
def reflect_y(self:'vector') -> 'vector reflected around y axis':
return self.flip().reflect_x()
See, if you look at how this function computes, it's actually quite trivial. But suddenly an amazing thing happened: I was able to write a transformation using only the existing transformations flip and reflect_x. For all, I care, reflect_y could be defined in a derived class without access to x and y and it would still work!
Mathematicians would call these functions operators. They would say that reflect_y is an operator obtained by composition of operators flip and reflect_x which is
denoted by reflect_y = flip ○ reflect_x (you should see the small circle, a Unicode symbol 25CB).
Note: I will quite freely use the = symbol from now to denote that two operations produce the same result, like in the paragraph above. This is a "mathematical =", which cannot be expressed as a program.
So if I do
print(v.reflect_y())
I get the result (-5, 3). Go and picture it!
Question: Consider a composition reflect_y ◦ reflect_y. How would you name it?
Rotations
Those operations were nice and useful, but you are probably wondering why am so slow to introduce rotations. Ok, here I go:
def rotate(self:'vector', angle:'rotation angle') -> 'vector':
??????
At this point, if you know how to rotate vectors, you should go on and fill in the question marks. Otherwise please bear with me for one more simple case: counterclockwise rotation by 90 degrees. This one is not hard to draw on a piece of paper:
def rotate_90(self:'vector') -> 'rotated vector':
new_x = - self.y
new_y = self.x
return Vector(new_x, new_y)
Trying
x_axis = Vector(1, 0)
y_axis = Vector(0, 1)
print(x_axis.rotate_90(), y_axis.rotate_90())
now gives (0, 1) (-1, 0). Run it yourself!
Question: Prove that flip = rotate_90 ◦ rotate_90.
Anyway, I won't hide the secret ingredient for longer:
import math # we'll need math from now on
...
class Vector:
...
def rotate(self:'vector', angle:'rotation angle') -> 'rotated vector':
cos = math.cos(angle)
sin = math.sin(angle)
new_x = cos * self.x - sin * self.y
new_y = sin * self.x + cos * self.y
return Vector(new_x, new_y)
Now let's try something along the lines:
print(x_axis.rotate(90), y_axis.rotate(90))
If you expect the same result as before, (0, 1) (-1, 0), you're bound to be disappointed. That code prints:
(-0.448073616129, 0.893996663601) (-0.893996663601, -0.448073616129)
and boy, is it ugly!
Notation: I will say that we applied operation rotate(90) to x in the example above. The knowledge we gained is that rotate(90) != rotate_90.
Question: What happened here? How to express rotate_90 in terms of rotate? How to express flip in terms of rotate?
Dilations
Those rotations are certainly useful, but they are not everything you need to do even the 2D graphics. Consider the following transformations:
def dilate(self:'vector', axe_x:'x dilation', axe_y:'y dilation'):
'''Dilates a vector along the x and y axes'''
new_x = axe_x * self.x
new_y = axe_y * self.y
return Vector(new_x, new_y)
This dilate thing dilates the x and y axes in a possibly different way.
Exercise: Fill in the question marks in dilate(?, ?) = flip, dilate(?, ?) = reflect_x.
I will use this dilate function to demonstrate a thing mathematicians call commutativity: that is, for every value of parameters a, b, c, d you can be sure that
dilate(a, b) ◦ dilate(c, d) = dilate(c, d) ◦ dilate(a, b)
Exercise: Prove it. Also, is it true that for all possible values of parameters those below would hold?
`rotate(a) ◦ rotate(b) = rotate(b) ◦ rotate(a)`
`dilate(a, b) ◦ rotate(c) = rotate(c) ◦ dilate(a, b)`
`rotate(a) ◦ __mul__(b) = __mul__(b) ◦ rotate(a)`
Matrices
Let's summarize all the stuff we had around here, our operators on vector x
flip, reflect_x, *, rotate(angle), dilate(x, y)
from which one could make some really crazy stuff like
flip ◦ rotate(angle) ◦ dilate(x, y) ◦ rotate(angle_2) ◦ reflect_y + reflect_x = ???
As you create more and more complicated expressions, one would hope for some kind of order that would suddenly reduce all possible expressions to a useful form. Fear not! Magically, every expression of the form above can be simplified to
def ???(self:'vector', parameters):
'''A magical representation of a crazy function'''
new_x = ? * self.x + ? * self.y
new_y = ? * self.x + ? * self.y
return Vector(new_x, new_y)
with some numbers and/or parameters instead of ?s.
Example: Work out what the values of '?' are for __mul__(2) ◦ rotate(pi/4)
Another example: Same question for dilate(x, y) ◦ rotate(pi/4)
This allows us to write a universal function
def transform(self:'vector', m:'matrix') -> 'new vector':
new_x = m[0] * self.x + m[1] * self.y
new_y = m[2] * self.x + m[3] * self.y
return Vector(new_x, new_y)
which would take any 4-tuple of numbers, called matrix, and apply it to vector x. Here's an example:
rotation_90_matrix = (0, -1, 1, 0)
print(v, v.rotate_90(), v.transform(rotation_90_matrix))
which prints (5, 3) (-3, 5) (-3, 5). Note that if you apply transform with
any matrix to origin, you still get origin:
origin = Vector(0, 0)
print(origin.transform(rotation_90_matrix))
Exercise: what are the tuples m that describe flip, dilate(x, y), rotate(angle)?
As we part with the Vector class, here's an exercise for those who want to test both their vector math knowledge and Pythonic skills:
The final battle: Add to the Vector class all vector operations that you can come up with (how many standard operators can you overload for vectors? Check out my answer).
Matrices: Overloaded
=
As we found out in the previous section, a matrix can be thought of as a shorthand that allows us to encode a vector operation in a simple way. For example, rotation_90_matrix encodes the rotation by 90 degrees.
Matrix objects
Now as we shift our attention from vectors to matrices, we should by all means have a class
for matrix as well. Moreover, in that function Vector.transform(...) above the role of the matrix was somewhat misrepresented. It's more usual for m to be fixed while vector changes, so from now on our transformations will be methods of matrix class:
class Matrix:
def __init__(self:'new matrix', m:'matrix data'):
'''Create a new matrix.
So far a matrix for us is just a 4-tuple, but the action
will get hotter once The (R)evolution happens!
'''
self.m = m
def __call__(self:'matrix', v:'vector'):
new_x = self.m[0] * v.x + self.m[1] * v.y
new_y = self.m[2] * v.x + self.m[3] * v.y
return Vector(new_x, new_y)
If you don't know Python, __call__ overloads the meaning of (...) for matrices so I can use the standard notation for a matrix acting on a vector. Also, the matrices are usually written using a single uppercase letter:
J = Matrix(rotation_90_matrix)
print(w, 'rotated is', J(w))
Exercise: repeat this example with matrices from the previous exercise.
Addition
Now, let's find out what else we can do with matrices. Remember that matrix m is really just a way to encode an operation on vectors. Note that for two functions m1(x) and m2(x) I can create a new function (using lambda notation) m = lambda x: m1(x) + m2(x). It turns out if m1 and m2 were encoded by matrices, you can also encode this m using matrices!
Exercise: Think through any difficulties you might have with this statement.
You just have to add its data, like (0, 1, -1, 0) + (0, 1, -1, 0) = (0, 2, -2, 0). Here's how to add two tuples in Python, with some very useful and highly Pythonic techniques:
def __add__(self:'matrix', snd:'another matrix'):
"""This will add two matrix arguments.
snd is a standard notation for the second argument.
(i for i in array) is Python's powerful list comprehension.
zip(a, b) is used to iterate over two sequences together
"""
new_m = tuple(i + j for i, j in zip(self.m, snd.m))
return Matrix(new_m)
Now we can write expressions like J + J or even J + J + J, but to see the results we have to figure out how to print a Matrix. A possible way would be to print a 4-tuple of numbers, but let's take a hint from the Matrix.__call__ function that the numbers should be organized into a 2x2 block:
def as_block(self:'matrix') -> '2-line string':
"""Prints the matrix as a 2x2 block.
This function is a simple one without any advanced formatting.
Writing a better one is an exercise.
"""
return ('| {0} {1} |\n' .format(self.m[0], self.m[1]) +
'| {0} {1} |\n' .format(self.m[2], self.m[3]) )
If you look at this function in action you'll notice there is some room for improvement:
print((J + J + J).as_block())
Exercise: write a nicer function Matrix.__str__ that will round the
numbers and print them in the fields of fixed length.
Now you should be able to write the matrix for rotation:
def R(a: 'angle') -> 'matrix of rotation by a':
cos = math.cos(a)
sin = math.sin(a)
m = ( ????? )
return Matrix(m)
Exercise: Examine the code for Vector.rotate(self, angle) and fill in the question marks. Test with
from math import pi
print(R(pi/4) + R(-pi/4))
Multiplication
The most important thing we can do with one-parameter functions is compose them: f = lambda v: f1(f2(v)). How to mirror that with matrices? This requires us to examine how Matrix(m1) ( Matrix(m2) (v)) works. If you expand it, you'll notice that
m(v).x = m1[0] * (m2[0]*v.x + m2[1]*v.y) + m1[1] * (m2[2]*v.x + m2[3]*v.y)
and similarly for m(v).y, which, if you open the parentheses, looks suspiciously similar
to Matrix.__call__ using a new tuple m, such that m[0] = m1[0] * m2[0] + m1[2] * m2[2]. So let's take this as a hint for a new definiton:
def compose(self:'matrix', snd:'another matrix'):
"""Returns a matrix that corresponds to composition of operators"""
new_m = (self.m[0] * snd.m[0] + self.m[1] * snd.m[2],
self.m[0] * snd.m[1] + self.m[1] * snd.m[3],
???,
???)
return Matrix(new_m)
Exercise: Fill in the question marks here. Test it with
print(R(1).compose(R(2)))
print(R(3))
Math exercise: Prove that R(a).compose(R(b)) is always the same as R(a + b).
Now let me tell the truth: this compose function is actually how mathematicians decided to multiply matrices.
This makes sense as a notation: A * B is a matrix that describes operator A ○ B, and as we'll see next there are deeper reasons to call this 'multiplication' as well.
To start using multiplication in Python all we have to do is to order it so in a Matrix
class:
class Matrix:
...
__mul__ = compose
Exercise: Compute (R(pi/2) + R(pi)) * (R(-pi/2) + R(pi)). Try to find the answer yourself first on a piece of paper.
Rules for + and *
Let's make some good name for the matrix that corresponds to the dilate(a, b) operator. Now there's nothing wrong with D(a, b), but I'll
use a chance to introduce a standard notation:
def diag(a: 'number', b: 'number') -> 'diagonal 2x2 matrix':
m = (a, 0, 0, b)
return Matrix(m)
Try print(diag(2, 12345)) to see why it's called a diagonal matrix.
As the composition of operations was found before to be not always commutative, * operator won't be always commutative for matrices either.
Exercise: go back and refresh the commutativity thing if necessary. Then give examples of matrices A, B, made from R and diag,
such that A * B is not equal to B * A.
This is somewhat strange, since multiplication for numbers is always commutative, and raises the question whether compose really deserves to be called __mul__. Here's quite a lot of rules that + and * do satisfy:
A + B = B + A
A * (B + C) = A * B + A * C
(A + B) * C = A * C + B * C
(A * B) * C = A * (B * C)
There is an operation called A - B and (A - B) + B = A
Exercise: Prove these statements. How to define A - B in terms of +, *, and diag? What does A - A equal to? Add the method __sub__ to the class Matrix. What happens if you compute R(2) - R(1)*R(1)? What should it be equal to?
The (A * B) * C = A * (B * C) equality is called associativity and is especially nice since it means that we don't have to worry about putting parentheses in an expression
of the form A * B * C:
print(R(1) * (diag(2,3) * R(2)))
print((R(1) * diag(2,3)) * R(2))
Let's find analogues to regular numbers 0 and 1 and subtraction:
zero = diag(0, 0)
one = diag(1, 1)
With the following easily verifiable additions:
A + zero = A
A * zero = zero
A * one = one * A = A
the rules become complete, in the sense that there is a short name for them: ring axioms.
Mathematicians thus would say that matrices form a ring, and they indeed always use symbols + and * when talking about rings, and so shall we.
Using the rules it's possible to easily compute the expression from the previous section:
(R(pi/2) + R(pi)) * (R(-pi/2) + R(pi)) = R(pi/2) * R(-pi/2) + ... = one + ...
Exercise: Finish this. Prove that (R(a) + R(b)) * (R(a) - R(b)) = R(2a) - R(2b).
Affine Transformations
Time to return to how we defined matrices: they are a shortcut to some operations you can do with vectors, so it's something you can actually draw. You might want to take a pen or look at the materials that others suggested to see examples of different plane transformations.
Among the transformations, we'll be looking for the affine ones, those who look 'the same' everywhere (no bending). For example, a rotation around some point (x, y) qualifies. Now this one cannot be expressed as lambda v: A(v), but it can be written in the form lambda v: A(v) + b for some matrix A and vector b.
Exercise: find the A and b such that a rotation by pi/2 around the point (1, 0) has the form above. Are they unique?
Note that for every vector there is an affine transformation which is a shift by the vector.
An affine transformation may stretch or dilate shapes, but it should do in the same way everywhere. Now I hope you believe that the area of any figure changes by a constant number under the transformation. For a transformation given by matrix A this coefficient is called the determinant of A and can be computed applying the formula for an area to two vectors A(x_axis) and A(y_axis):
def det(self: 'matrix') -> 'determinant of a matrix':
return self.m[0]*self.m[3] - self.m[1] * self.m[2]
As a sanity check, diag(a, b).det() is equal to a * b.
Exercise: Check this. What happens when one of the arguments is 0? When it's negative?
As you can see, the determinant of rotation matrix is always the same:
from random import random
r = R(random())
print (r, 'det =', r.det())
One interesting thing about det is that it is multiplicative (it kind of follows from the definition if you meditate long enough):
A = Matrix((1, 2, -3, 0))
B = Matrix((4, 1, 1, 2))
print(A.det(), '*', B.det(), 'should be', (A * B).det())
Inverse
A useful thing you can do with matrices is writing a system of two linear equations
A.m[0]*v.x + A.m[1]*v.y = b.x
A.m[2]*v.x + A.m[3]*v.y = b.y
in a simpler way: A(v) = b. Let's solve the system as they teach in (some) high schools: multiply the first equation by A.m[3], second by -A.m1, and add (if in doubt, do this on a piece of paper) to solve for v.x.
If you really tried it, you should have got A.det() * v.x = (A.m[3]) * b.x + (-A.m[1]) * b.y, which suggests that you can always get v by multiplying b by some other matrix. This matrix is called inverse of A:
def inv(self: 'matrix') -> 'inverse matrix':
'''This function returns an inverse matrix when it exists,
or raises ZeroDivisionError when it doesn't.
'''
new_m = ( self.m[3] / self.det(), -self.m[1] / self.det(),
????? )
return Matrix(new_m)
As you see, this method fails loudly when the determinant of a matrix is zero. If you really want you can catch this exception with:
try:
print(zero.inv())
except ZeroDivisionError as e: ...
Exercise: Finish the method. Prove that inverse matrix doesn't exist when self.det() == 0. Write the method to divide matrices and test it. Use the inverse matrix to solve an equation A(v) = x_axis (A was defined above).
Powers
The main property of inverse matrix is that A * A.inv() always equals to one
Exercise: check that yourself. Explain why that should be so from the definition of the inverse matrix.
That's why mathematicians denote A.inv() by A-1. How about we write a
nice function to use A ** n notation for An? Note that the naive for i in range(n): answer *= self cycle is O(|n|) which is certainly too slow, because
this can be done with a complexity of log |n|:
def __pow__(self: 'matrix', n:'integer') -> 'n-th power':
'''This function returns n-th power of the matrix.
It does it more efficiently than a simple cycle. A
while loop goes over all bits of n, multiplying answer
by self ** (2 ** k) whenever it encounters a set bit.
...
Exercise: Fill in the details in this function. Test it with
X, Y = A ** 5, A ** -5
print (X, Y, X * Y, sep = '\n')
This function only works for integer values of n, even though for some matrices we can also define a fractional power, such as square root (in other words, a matrix B such that B * B = A).
Exercise: Find a square root of diag(-1, -1). Is this the only possible answer?
Find an example of a matrix that doesn't have a square root.
Bonus: Complex numbers
Here I'm going to introduce you to the subject in exactly one section!
Since it's a complex subject, I'm likely to fail, so please forgive me in advance.
First, similarly to how we have matrices zero and one, we can make a matrix out of any real number by doing diag(number, number). Matrices of that form can be added, subtracted, multiplied, inverted and the results would mimic what happens with the numbers themselves. So for all practical purposes, one can say that, e.g., diag(5, 5) is 5.
However, Python doesn't know yet how to handle expressions of the form A + 1 or 5 * B where A and B are matrices. If you're interested, you should by all means go and do the following exercise or look at my implementation (which uses a cool Python feature called decorator); otherwise, just know that it's been implemented.
Exercise for gurus: Change the operators in a Matrix class so that in all standard operations where one of the operands is a matrix and another a number, the number is automatically converted to the diag matrix. Also, add a comparison for equality.
Here's an example test:
print( 3 * A - B / 2 + 5 )
Now here's the first interesting complex number: the matrix J, introduced in the beginning and equal to Matrix((0, 1, -1, 0)), has a funny property that J * J == -1 (try it!). That means J is certainly not a normal number, but, as I just said, matrices and numbers easily mix together. For example,
(1 + J) * (2 + J) == 2 + 2 * J + 1 * J + J * J = 1 + 3 * J
using the rules listed sometime before. What happens if we test this in Python?
(1 + J) * (2 + J) == 1 + 3*J
That should happily say True. Another example:
(3 + 4*J) / (1 - 2*J) == -1 + 2*J
As you might have guessed, the mathematicians don't call those 'crazy numbers', but they do something similar - they call expressions of the form a + b*J complex numbers.
Because those are still instances of our Matrix class, we can do quite a lot of operations with those: addition, subtraction, multiplication, division, power - it's all already implemented! Aren't matrices amazing?
I have overlooked the question of how to print the result of an operation like E = (1 + 2*J) * (1 + 3*J) so that it looks like an expression with J rather than a 2x2 matrix. If you examine it carefully,
you'll see that you need to print the left column of that matrix in the format ... + ...J (just one more nice thing: it's exactly E(x_axis)!) Those who know the difference between str() and repr() should see it's natural to name a function that would produce expression of such form as repr().
Exercise: Write the function Matrix.__repr__ that would do exactly that and try some tests with it, like (1 + J) ** 3, first computing the result on paper and then trying it with Python.
Math question: What is the determinant of a + b*J? If you know what the absolute value of a complex number is: how they are connected? What is the absolute value of a? of a*J?
Matrices: The (R)evolution
=
In the final part of this trilogy, we will see that everything is a matrix. We'll start with general M x N matrices, and find out how vectors can be thought of as 1 x N matrices and why numbers are the same as diagonal matrices. As a side note, we'll explore the complex numbers as 2 x 2 matrices.
Finally, we will learn to write affine and projective transformations using matrices.
So the classes planned are [MNMatrix, NVector, Affine, Projective].
I guess if you were able to bear with me until here, you could be interested in this sequel, so I'd like to hear if I should continue with this (and where, since I'm pretty much sure I'm beyond what considered a reasonable length of a single document).
MIT has alot of their math courses' materials online at http://ocw.mit.edu/OcwWeb/Mathematics/. Once you have the basics down, they have the physics notes online too.
this is http://en.wikipedia.org/wiki/Computer_graphics. two of the key concepts are http://mathworld.wolfram.com/LinearTransformation.html, and http://mathworld.wolfram.com/AffineTransformation.html.
This MIT document is a must-have to get strong knowledge on the basics of Transformation.
http://stellar.mit.edu/S/course/6/fa08/6.837/courseMaterial/topics/topic2/lectureNotes/03_transform/03_transform.pdf
One of the best books for beginners is Carl Meyer's "Matrix Analysis and Applied Linear Algebra".
You can view the entire book online here (although it has a copyright watermark):
http://www.matrixanalysis.com/DownloadChapters.html
You might want to take a look at Chapter 5 pg. 326 - 332 which covers the rotations in 3 dimensional computer graphics
You may want to look at Geometric linear algebra by I-Hsiung Lin, Yixiong Lin (ISBN : 9812560874). The book is specifically geared towards what you want (linear transformations of 2 and 3-dimensional vector spaces) and treats it with a geometric approach in full, progressive detail (300 pages for each dimension). I'm afraid it's not free to buy, but you should be able to find it in any good university library. Otherwise Bookfinder should help you get it at a relatively modest price.
Jim Hefferon's free Linear Algebra textbook is really good. Unlike too many free ebooks, Jim has clearly taken the time to craft an excellent reader and introduction to linear algebra. It's not overly burdened with formal mathematical writing, which is often too dense with theorems and proofs to be easily comprehensible. It also contains lots of really excellent examples of real world applications of linear algebra - coordinate transformations being just one example. You can't beat the price, and it also comes with optional solutions to the exercises.
P.S. If coordinate transformations are your thing, you might be interested in differential geometry after you're done with linear algebra.
Those are the information that I found. Some of them might be valuable to You:
Theory:
Woflrams Information about matrices and additional ones on Wikipedia
Matrices by Pam Norton - very good book, available at Google Books for free.
Orthographic and perspective projections
3D transformations at MSDN
(Searching for "Matrices" at Google books gives You lots of lecutures, some of which are directly connected with transformations - this is one of the first results, but I cheer You to check more.)
Articles like this one, or maybe this or this are easily found with help of Google, so I will not post more.
There were also few questions about matrices on StackOverflow.com : Using 3d transformation matrices and How to apply a transformation matrix? are just first few examples, You can find more looking for the matrix or math tags.
I also encourage (I don't know if this is the right word, I am just learning English) You, to look for this kind of information in one of those books (though they are not free, but You can find large parts of older ones on Google Books):
Game programming gems 7
Game programming gems 6
Game programming gems 5
Game programming gems 4
Game programming gems 3
Game programming gems 2
Game programming gems
Each of those has section about math gems - and there are lots of neat tricks there. Those books are worth every cent.
There are also GPU Programming gems, so You might try them too.
Practice:
OpenGl Book at Wikipedia has section about matrices in OpenGl
(also, using matrices in OpenGl is described here and here)
Drawing 3D room in DirectX + info about matrix transformations.
If I will find more, I will edit and add links here, but to be honest - I found those links in about 10 minutes of using google. World's most popular browser stores data about everything - and yes, "everything" means matrices too.
Cheers, Mate.
I think you should spend a few days doing dot products and cross products with vectors in 3D. Then learn the relation between trig and vectors. After that the matrices will make a lot more sense to you.
MIT-OCW's course on Linear Algebra by Gilbert Strang. Incredible lectures by an incredible man; if your understanding of matrices is solely derived from programming sources (like MATLAB) then a course on Linear Algebra will definitely give you the fundamentals to do crazy things with matrices.
http://www.ocw.cn/OcwWeb/Mathematics/18-06Spring-2005/VideoLectures/index.htm

Resources