Monday, 13 January 2014

Typical degree problem & Python: Part 3

Problem 3 formulation:

Given a line described by ax+by+c=0, create a function script with the values a,b,c as input that prints:

1-The affine transformation matrix obtained by creating a symmetry respect to the line

2-A plot of a circle with 1 unit radius centered on (0,0) and its reflection



Solution:

##### Libraries #####

from numpy import *

import math as mt # Mistake corrected

import pyqtgraph as pg

pg.setConfigOption('background','w') # Changes plot window background to white

####################

def tafin(a,b,c):       # function declaration
  window=pg.plot(title="Reflection") # Creates plot window called window
  Ax=linspace(-5,5)    # Generates values from -5 to 5
  Ay=-a*Ax/b-c/b       # Line equation
  window.plot(Ax,Ay)       # Plots line
  alpha=linspace(0,2*pi) # Generates alpha values
  Bx=cos(alpha)          # X coordinates of the (0,0) centered circumference 
  By=sin(alpha)          # Y coordinates of the (0,0) centered circumference 
  window.plot(Bx,By,pen='r') # Plots (0,0) centered circumference in red colour
  B1s=ones((1,size(Bx)))        # Line vector filled with 1's
  F=vstack((Bx,By,B1s)) # Compounds an array from Bx,By and B1s
  XA=0     # random point
  YA=(-a/b)*XA-(c/b)     # Obtains one point of the symetry line
  XB=1     # another random point
  YB=(-a/b)*XB-(c/b)     # Obtains another point of the symetry line
  # Symetry matrix
  Ta1=array(([XA,XB,-a],[YA,YB,-b],[1,1,0])) # Mirrored points coincident with line
  Ta2=linalg.inv(array(([XA,XB,a],[YA,YB,b],[1,1,0])))  # Original line coincident points
  Ta=dot(Ta1,Ta2) # Symetry matrix
  S=dot(Ta,F)     # Mirrors the circumference coordinates stored in F
  window.plot(S[0,:],S[1,:],pen='b') # Plots mirrored circumference in blue
  print "The affine transformation matix is"  #
  print Ta   # prints affine transformation matrix 



Open a terminal, paste the function and then type something like tafin(2.0,1.0,4.0)  the output should be very similar to the screenshot. 

Solving this problem with python has allowed me to understand more things about this language, for example, I used to import math as * alongside with numpy as *, this should not be done. The first thing where you can see how that affects to coding is the absence of "for" bucles to operate within arrays and their elements. 
This clearly makes the code easier, cleaner and more efficient and shorter than the matlab equivalent.

The new thing with the pyqtgraph module are the color plots, and plot background, easy to perform. 


With this problem I finish this little section about algebra and python, hope that you got something good from it. 

Bye!

Sunday, 5 January 2014

Typical degree problem & Python: Part 2

Problem 2 formulation:

Create a script that plots an n vertexes polygon inscribed in an ellipse with semi axis a=7, b=2. Solve for n=12


Solution:

##### Libraries #####

from numpy import *


import pyqtgraph as pg


#####################


a=7 # Semi axis X value


b=2 # Semi axis Y value 


n=12 # Polygon vertex number


window=pg.plot(title="Inscribed Polygon") #Creates plot window called window


alpha=linspace(0,2*pi) # Creates line matrix with values from 0 to 2*pi


SA=size(alpha) # Gets the size of alpha matrix


E=zeros([2,SA]) # Creates a 2xSA matrix filled with zeros


for i in range(SA): # Gets X,Y values of the ellipse and stores them in E[0,i]

  E[0,i]=a*cos(alpha[i])
  E[1,i]=b*sin(alpha[i])

window.plot(E[0,:],E[1,:]) # Plots the ellipse


V=zeros([2,n+1]) # Array filled with zeros that will contain polygon vertexes

alpha=0 # Variable reset

for i in range (n+1): # Bucle that obtains the coordinates of an n vertexes polygon

  V[0,i]=cos(alpha)
  V[1,i]=sin(alpha)
  alpha+=2*pi/n

VSM=[[a,0],[0,b]] # Transformation matrix that will inscribe the polygon inside the ellipse


V=dot(VSM,V) # Transformation matrix * Polygon


window.plot(V[0,:],V[1,:]) # Plots the polygon





To see it working, open a terminal (ctrl+alt+t), type "python" and then copy-paste the code.(you need to have installed the pyqtgraph module in order to plot, see this post)

This problem is very similar to the Problem 1 and without the importing part it is almost equal to the matlab script.


Next post will be about mirroring an image with a line equation as a mirror line. 

Stay tuned, bye!

Typical degree problem & Python: Part 1

Recently I've been forced to use Matlab for solving some algebra exercises for my degree.
Matlab drives me mad in several ways and even before typing the first line of code, I've been thinking about solving the problems with python.

Let's start.

Problem 1 formulation:

Draw an ellipse with its semi axes measuring a=5 and b=2, then create a script that plots a number n of ellipses equal in size with common center, solve for n=30. Each ellipse should be rotated an angle of pi/n radian from the previous one. The output of the script should look like this:

Solution:

##### Libraries #####

from numpy import *


from math import *


import pyqtgraph as pg


#####################


##### Input Variables


a=7  # semi axis X value


b=2  # semi axis Y value


n=30 # number of ellipses


#####################


window=pg.plot(title="Concentric Ellipses") #Creates plot window called window


alpha=linspace(0,2*pi) # Creates line matrix with values from 0 to 2*pi


SA=size(alpha) # Gets the size of alpha matrix


E=zeros([2,SA]) # Creates a 2xSA matrix filled with zeros


for i in range(SA): # Gets X,Y values of the ellipse and stores them in E

  E[0,i]=a*cos(alpha[i])
  E[1,i]=b*sin(alpha[i])

beta=0 # Ellipse angle variable inicialization


for i in range(n): # Generates n concentric ellipses angled pi/n radians 

  
  TM=[[cos(beta),-sin(beta)],[sin(beta),cos(beta)]] # Transformation matrix
     
  RE=dot(TM,E) # RE contains the coordinates of the rotated ellipse
  
  REX=RE[0,:] # RE X values extraction
  REY=RE[1,:] # RE Y values extraction
  
  window.plot(REX,REY) # Plots the current rotated ellipse
  
  beta+=pi/n # Increases beta by pi/n steps
  


To see it working, open a terminal (ctrl+alt+t), type "python" and then copy-paste the code.
(you need to have installed the pyqtgraph module in order to plot, see this post)

The main difference between python and matlab scripts for this exercise is the library import header, being the rest of them very mimetic.


Second exercise on the next post, bye!