Skip to content

Metric Tensor in Quantity System

April 29, 2012

The best part in making the Quantity System, is that I am re-discovering mathematics with it. It gives me an immense pleasure to learn mathematics in a practical way and to try and make corrections on two levels, Programming and Mathematics.

This post will be a usage scenario for the quantity system. The scenario will be applicable for the latest source code (which can be downloaded from labeled by version [Qs 1.2.5])

In this post I will explain the mathematical foundation with my language/realization (so I am not tied to the formal way of books)

Coordinates Introduction

In studying geometry, we always encounter (x,y,z) coordinate system. This system simply is a way to describe the location of point.

And if we are talking with Vectors, then {x y z} is an arrow coming from {0 0 0} to {x y z} and we are calling it Vector.

Using {x1 x2 x3} labeling instead of {x y z} then we have indexes 1,2, and 3 usually referred as R3 Space.

What does that mean ??

R2: 2 dimensional coordinate system (2D Space)

R3: 3 dimensional coordinate system (3D Space)

R4: 4 dimensional coordinate system (4D Space) (yes it exists somehow refer to Hypersphere coordinate system )

Note: Using ‘R’ refer to the numbering system type we are using which is Real Numbers.

So in Rn the point/vector can be described by {x1 x2 x3 x4 .. xn} and to generalize the point location we end into defining xi

where i: 1,2, 3, .., n for number of coordinates

Cartesian Coordinates

They are simply {x y} or {x y z}, R2 or R3 which we all know and studied during school. (an important note here is that the angle between the unit vectors is 90 degree or Orthogonal)

Curvilinear Coordinates

In this type of coordinates the unit vectors that represent the coordinate system is not a straight forward lines, but on contrary they are making curves in some way.

When dealing with curvilinear coordinates every coordinate has a tangent vector (e) and orthogonal vector (E)

Without going into a lot of details these extra vectors are the foundation of covariant and contravariant vectors

The curse of Curvilinear coordinates is that the location of point can be represented by two vectors covariant or contravariant.

We label the two types of vectors by this

Contravariant vector: xi superscript index or simple {x1 x2 x3}

Covariant vector: xi subscript index {x1 x2 x3}

Best to be seen by the following figure

Note: we are obliged to use two types of vectors specially when we are beginning to use geometric properties of geometric objects (for example the line length in curvilinear coordinates is not the same as Cartesian coordinates, think about line over spherical object like big line on earth) line length = sqrt(x*x + y*y + z*z) however in curvilinear length = sqrt(x1*x1 + x2*x2 +x3*x3) or simply length = sqrt(xi*xi) Einstein summation notation.

Coordinates Transformation

We can transform the point from Cylindrical to Cartesian (here I mean changing the representation of point/vector from r,q,z to x,y,z )

x = r cos(q)

y = r sin(q)

z = z

a vice versa transformation exist but I will not write it here

for simplicity let us write that any {r q z} vector we can transform it by multiplying it with {r*cos(q) r*sin(q) z}

so for any two coordinate systems there is a transformation

Note: in mathematical books they are referring to Jacobian calculation to make sure that such transformation exist

How can we make this simple with quantity system?

  • Define a vector that contains a symbolic quantities
  • Get the function from this vector
  • Now you can get x, y, z vector from r q z vector by the function fc
  • Also using named parameters (so you don’t get ambiguous about parameters order)
  • And for fun you can pass Symbolic parameters (remember that quantity system blend the usage between numbers and symbols)


Note: Quantity system permits you to make calculations on symbols as if they were first citizen types in Qs to define symbol you can precede dollar sign before it like $x $y $myvariable and to define complex expression you can use ${expression} like ${x+y*r} or ${sqrt(sin(u*v^2))} please refer to the separate symbolic variable engine that also made by me in

Covariant and Contravariant vectors Dilemma

A question is raised here, how can I get covariant and contravariant components of vector with the guide of Transformation Equations and existing vector?

This dilemma is solved by the Metric Tensor. The metric tensor is a matrix like object that when multiplied by vector gets the corresponding vector

There are also two types of metric tensor (covariant metric tensor and contravariant metric tensor)

To calculate the metric tensor we get back to the definition of base vector

In the figure above the x1,x2,x3 represent a curvilinear coordinate system and g1,g2,g3 represent covariant base vectors (the base vectors has been defined by the differentiation of the location point transformation of curvilinear coordinate system)

Let us take the cylindrical transformation to Cartesian

x = r cos(q)

y = r sin(q)

z = z

g1 = g1x*i1+g1y*i2+g1z*i3

to get these components we differentiate each term of transformation vector with r

g1x = diff(r*cos(q), r)

g1y = diff(r*sin(q), r)

g1z = diff(z, r)

then we get the other

g2 also is the differentiation of all components with q

g3 is differentiating with z

Now to make the metric tensor we will use the dot product of vectors to get this 2nd order tensor which looks like matrix











I will not dive into details about characteristics of metric tensor but there are common rules about them

  1. contravariant * covariant metric tensors = Identity
  2. Covariant Metric Tensor * Covariant Vector = Contravariant Vector
  3. Contravariant Metric Tensor * Contravariant Vector = Covariant Vector.

So by getting the metric tensor for the curvilinear coordinate system (or any higher spaces if we want) then we can get the covariant and contravariant components of vector.

Metric Tensor with Quantity System

As my reader can see, you have to know many skills to get the metric tensor for specific coordinate system

  • Transformation equations
  • Differentiations
  • Dot Product
  • Matrix Algebra (for matrix inverse)

Without another mumbling let’s use the precious quantity system for this operation.

Continuing on the last transformation for cylindrical coordinates

  • Get g1
  • as it can be noticed differentiation has been made all over components of vector
  • Get g2
  • Get g3
  • Get the covariant metric tensor

You can notice that if you do Trigonometric simplification to the covariant metric tensor of cylindrical coordinates would be











Which is the right deduction for it (I didn’t implement simplification in symbolic algebra yet bear with me please J)

Calculating contravariant vector


  • Get a function from the metric tensor
  • Suppose that we have vector with r=3 theta=2 and z=10 as a covariant vector
  • Then to get contravariant vector we simply do the following



  • Quantity system make it easy to calculate the metric tensor
  • Quantity system make it easy to get covariant and contravariant vectors
  • I am very biased J
    J to the quantity system (I can’t help it actually)


Of course I am not claiming that my implementation is 100% error free (and you will see mistakes while playing with these calculations) however I am trying to say that the idea of blending symbolic calculations beside numerical calculations and getting the result easily and intuitively should enhance the global or macro view of mathematics and may increase the researcher ability to distinguish between these concepts and testing it easily while he is studying.

I also wish that I can reach a stable state that makes these calculations really reachable and easy for all of those interested beside being easy in using as an embedded language inside C# applications and .NET framework.


Thank you all for reading this so far.


Ahmed Sadek











From → Uncategorized

One Comment

Trackbacks & Pingbacks

  1. Fantastic Mesh My Master Software | Fantastic Mesh

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: