Created by Ryo Kurashina
Email: rk2014@ic.ac.uk
HTML Version (This will be a link)


Parallelepipeds

Learning Objectives

  • Understand what a parallelpiped is and know how the triple scalar product is related to its volume.

Contents

  1. Introduction
  2. Triple Scalar Product
    1. Cross Product and Area of Parallelograms
    2. Volume of a Parallelepiped
  3. Creating a Parallelepiped Class and Methods in Python

1. Introduction

A parallelepiped is a shape in 3D that is uniquely defined by 3 vectors $\vec{u},\,\vec{v}$ and $\vec{w}$. On this IPython Notebook we will see how the triple scalar product is related to the volume of a parallelepiped and write some code to plot and calculate its volume.

2. Triple Scalar Product

Before we explain the definition of the triple scalar product and how it's linked to the volume of a parallelepiped, we first stick to 2D and look at just two vectors $\vec{u}$ and $\vec{v}$.

2.A. Cross Product and Area of Parallelograms

Consider two vectors in 2D, $\vec{u}$ and $\vec{v}$, which form a parallelogram. If we want to work out the area it would simply be: $$ A = \,\mid\mid\vec{v}\mid\mid\cdot\mid\mid\vec{u}\mid\mid\mid\sin\theta\mid=\mid\vec{u}\times\vec{v}\,\mid$$
Notice that this is just the height of the parallelogram multiplied by its width. The term above is also exactly equal to the absolute value of the cross product of $\vec{u}$ and $\vec{v}$ (this is an alternative way to represent the cross product instead of the regular determinant definition).

2.B. Volume of a Parallelepiped

Now we know what the area of a parallelogram is let's extend our parallelogram to 3D and form a parallelepiped. The volume of the parallelepiped is simply the area of the base of the parallelepiped multiplied by it's height. So if we look at our diagram we can see that the height of the parallelepiped is equal to $\mid\mid\vec{w}\mid\mid\,\mid\cos\theta\,\mid$. So in total, the volume is equal to:

$$ V =\, \mid\vec{u}\times\vec{v}\mid \cdot \mid\mid\vec{w}\mid\mid\,\mid\cos\theta\mid \,=\, \mid\vec{w}\cdot(\vec{u}\times\vec{v})\,\mid$$
This is the triple scalar product and we find that the volume of a parallelepiped is exactly equal to the absolute value of the triple scalar product.

If the three vectors we choose are coplanar (i.e. they all lie on the same plane) it is easy to see that the volume of the parallelepiped becomes zero. Mathematically, we would get that the cross product of two of the vectors would be exactly perpendicular to the remaining vector which would give a zero dot product.

3. Creating a Parallelepiped Class and Methods in Python

The next section relies on knowledge about Classes in Python. If you're not familiar with classes then this is a good introduction.


In [ ]:
# Import packages and modules to be used 
import numpy as np
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)

In [ ]:
class Parallelepiped(object): 
    # Initialize a parallelepiped object by giving it 3 vector arguments
    def __init__(self,u,v,w):
        self.u = np.array(u)
        self.v = np.array(v)
        self.w = np.array(w)
    # Calculates the volume of a parallelepiped        
    def volume(self):
        crossp = np.cross(self.v,self.w)
        tp = np.dot(self.u,crossp)
        return abs(tp)
    def gopped(self):
        x = [0,self.u[0],self.u[0]+self.v[0],self.v[0],self.w[0],self.w[0]+self.u[0],self.w[0]+self.u[0]+self.v[0],\
             self.v[0]+self.w[0]]
        y = [0,self.u[1],self.u[1]+self.v[1],self.v[1],self.w[1],self.w[1]+self.u[1],self.w[1]+self.u[1]+self.v[1],\
             self.v[1]+self.w[1]]
        z = [0,self.u[2],self.u[2]+self.v[2],self.v[2],self.w[2],self.w[2]+self.u[2],self.w[2]+self.u[2]+self.v[2],\
             self.v[2]+self.w[2]]
        pped = go.Data([go.Mesh3d(x=x,y=y,z=z,
            i = [0, 0, 3, 4, 4, 4, 4, 4, 5, 6, 6, 7],
            j = [2, 3, 4, 3, 6, 7, 1, 5, 2, 2, 7, 3],
            k = [1, 2, 0, 7, 5, 6, 0, 1, 1, 5, 2, 2],opacity = 0.6)
            ])
        return pped
    # Method which finds a suitable layout for the pped when plotted
    def lytpped(self):
        if self.volume()==0:
            return ValueError("Vectors must not be coplanar")
        else:
            ubx = sum([abs(self.u[0]),abs(self.v[0]),abs(self.w[0])])
            uby = sum([abs(self.u[1]),abs(self.v[1]),abs(self.w[1])])
            ubz = sum([abs(self.u[2]),abs(self.v[2]),abs(self.w[2])])
            layout = go.Layout(
                title = "Plot of Parallelepiped",
                scene = dict(xaxis=dict(
                    title='x', range=[-ubx,ubx]
                ),
                yaxis=dict(
                    title='y',range=[-uby,uby]
                ),
                zaxis = dict(
                    title='z',range=[-ubz,ubz])
                )             
            )
            return layout

Now we give an example of how to use the Parallelepiped object.


In [ ]:
# Initialize the object with 3 vectors
my_pped = Parallelepiped([4.,0.,6.],[2.,-1.,1.],[4.,2.,3.])
# Find the volume by calling the .volume() method on it 
print(my_pped.volume())
# Find the Mesh3d data for the plot of the pped
data = my_pped.gopped()
# Find the Mesh3d layour for the plot of the pped
layout = my_pped.lytpped()

# Plot
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='parallelepiped')