php/Math   
Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 PHP/ir
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Hunch.net
 Matthew Hurst
 JMLR
 JSS
 Hal Daume III
 Math Notes >> Space & Physics

Powers of ten [Space & Physics
Posted on May 27, 2008 @ 11:05:40 PM by Paul Meagher

There is a celebrated book called "Powers of Ten" that offers up snapshots of what the universe looks like as you decrease or increase your magnification by successive powers of ten.

The cognitive scientist, Allan Newell, applied the "Powers of Ten" concept to the time dimension and observed that human action can be explained by biological, cognitive, rational and social constructs that generally operate at different time scales (Pirolli, p 19).

Table 1. Time scale on which human action occurs.

Scale (seconds)Time UnitBand
107MonthsSocial
106Weeks 
105Days 
104HoursRational
10310 Minutes 
102Minutes 
10110 SecondsCognitive
1001 Second 
10-1100 Milliseconds 
10-21 MillisecondBiological

Permalink 

Towards a Math_Geometry package [Space & Physics
Posted on January 22, 2007 @ 03:33:58 AM by Paul Meagher

A Math_Geometry package can be organized in many different ways. Once common way to organize such a library would be along 2D versus 3D lines (i.e., Math_Geometry_2D, Math_Geometry_3D). Another way to organize such a library would be in terms of the format in which geometric objects are represented. A line, for example, can be represented in parametric, implicit, or explicit formats which entails a different list of constuctor arguments/types and algorithms for computing common line properties (e.g., getDistanceToPoint(), getFoot(), isCollinear(), etc...). A format-based Geometry package might have classes/packages called Math_Line_Parametric, Math_Line_Explicit, and Math_Line_Implicit. The main argument for a format-based Geometry package is that some line properties are easier to compute when represented in certain ways. A collection of format-based classes would invoke each others methods to compute a line property using the most straightforward method.

These are preliminary observations about the possible requirements for a php-based Math_Geometry package. The purpose of the discussion is to suggest that the 2D versus 3D aspect of geometry is not necessarily the primary dimension to use in organizing a Math_Geometry package. We should also consider representational issues and remember that different formats are better adapted to solving different types of problems and perhaps a format-based organizational structure should be the primary organizational dimension for a Math_Geometry package. An ideal Math_Geometry package might also be a compromise between these organizational principles (e.g., vector-based arguments to parametric line methods would compute 2D verus 3D properties depending on the length of the supplied arrays).

Permalink 

Computing the area of a polygon [Space & Physics
Posted on November 23, 2006 @ 10:48:23 PM by Paul Meagher

The function below computes the area of a polygon given the x and y coordinates for each vertex of the polygon:

<?php
/**
* Compute the area of the specfied polygon.  
*
* Ported from link below.

* @see http://geosoft.no/software/geometry/Geometry.java.html
*
* @param array $x x coordinates of a polygon
* @param array $y y coordinates of a polygon   
*
* @return float area of specified polygon
*/
function computePolygonArea($x$y) {    
  
$n     count($x); 
  
$area  0.0;  
  for (
$i=0$i $n-1$i++)
    
$area += ($x[$i]*$y[$i+1])-($x[$i+1]*$y[$i]);    
  
$area += ($x[$n-1]*$y[0]) - ($x[0]*$y[$n-1]);    
  
$area *= 0.5;  
  return 
$area;
}

// coordinates for a 2w by 2h square
$x = array(2,4,4,2);
$y = array(2,2,4,4);

echo 
computePolygonArea($x$y);

// answer is 4
?>

If you follow the @see link in the code above you will encounter java methods for computing other attributes of a polygon (e.g., the centroid, the perimeter).

Exercises

1. Create a Math_Polygon class that allows you to easily compute several attributes of a polygon given its x and y coordinates.

2. Would the computePolygonArea() function be useful for computing the integral of some function (i.e., the area under the curve)? How might it be applied and how accurate would it be relative to other methods for approximating the integral?

3. A group of polygons which are connected together by shared vertices is referred to as a mesh. Would you be able to compute the volume of a solid from its mesh representation (i.e., computeMeshVolume)?

Permalink 

Mapping between unit square and target box coordinates [Space & Physics
Posted on October 30, 2006 @ 02:39:10 PM by Paul Meagher

Spatial software often works in a unit square coordinate system that maps the results to a target box coordinate sytem (i.e., unit2box) where they are viewed. Conversely, we may click on an object within a target box and convert the click coordinates to the underlying unit square coordinates (i.e., box2unit) to figure out which object was clicked.

The unit2box and box2unit functions below compute these mappings and work for a 2D or 3D coordinate system.

<?php
/**
* unit2box
*  
* Transforms unit square coordinates to target box coordinates.  Works
* in 2D and 3D contexts.
*  
* @param array $min coordinates for lower left corner of target box
* @param array $min coordiantes upper right corner of target box
* @param array $u unit coordinates to be transformed 
* @returns array $x of target box coordinates
*/
function unit2box($min$max$u) {
  
$n count($min);
  for(
$i=0$i<$n$i++)
    
$x[$i] = (1-$u[$i])*$min[$i] + $u[$i]*$max[$i]; 
  return 
$x;
}

/**
* box2unit
*  
* Transforms target box coordinates to unit square coordinates.  Works
* in 2D and 3D contexts.
*  
* @param array $min coordinates for lower left corner of target box
* @param array $min coordiantes upper right corner of target box
* @param array $x target box coordinates to be transformed 
* @returns array $u of unit square coordinates
*/
function box2unit($min$max$x) {
  
$n count($min);
  for(
$i=0$i<$n$i++)
    
$u[$i] = ($x[$i]-$min[$i])/($max[$i]-$min[$i]);
  return 
$u;
}

// set min and max coordinates
$min = array(101);
$max = array(212);

// set unit coordinates 
$u   = array(-1, -1, -1);
$x   unit2box($min$max$u);

// print target box x coordinates
echo "<pre>";
print_r($x);
echo 
"<pre>";

// clear u before recomputing it
$u   = array();

// feed in computed x to get back original u 
$u   box2unit($min$max$x);

// print original unit square coordinates
echo "<pre>";
print_r($u);
echo 
"<pre>";

// output 
/*
Array
(
    [0] => 0
    [1] => -1
    [2] => 0
)

Array
(
    [0] => -1
    [1] => -1
    [2] => -1
)
*/
?>

Permalink 

New Transform_Polar class [Space & Physics
Posted on October 16, 2006 @ 01:37:50 PM by Paul Meagher

I created a class to house some methods for working with polar coordinates. The class is called Transform_Polar because I have run into the need for a collection of transform methods before (i.e, Transform_Regression) and the collection of these transforms might eventually become a useful Math_Transform package.

To interpret the output of these methods you need to think in radians as the output of the php atan function, for example, is in radians which means $theta is in radians.

Here is what the Transform_Polar class looks like so far:

<?php
/**
* @package Math_Transform
* @class Tranform_Polar
* @path Transform/Polar.php
*
* @about Contains a library of polar coordinate methods.
*
* @see http://www.kralidis.ca/gis/compcart/geomath.pm
* @see http://www.mathworks.com/access/helpdesk/help/techdoc/ref/index.html?/access/helpdesk/help/techdoc/ref/cart2sph.html
* @see http://ca3.php.net/atan
* @see http://ca3.php.net/atan2
* @see http://users.powernet.co.uk/kienzle/octave/matcompat/scripts/specfun/cart2sph.m
*
* @author Paul Meagher
* @modified 2006-10-16
* @version 0.2
*/
class Transform_Polar {
  
  var 
$_atan "atan2";
  
  
/** 
  * Alternate PHP implementations of the arc tangent function.
  */
  
function arctan($x$y) {
    if (
$this->_atan == "atan")
      return 
atan($y/$x);  
    elseif (
$this->_atan == "atan2")
      return 
atan2($y$x);
    else
      die(
"Error: _atan not set"); 
  }
  
  
/**   
  * Transform Cartesian to polar coordinates.
  */
  
function cart2pol() {  }
  
  
/*
  * Transform Cartesian to spherical coordinates.
  */
  
function cart2sph($x$y$z) {
    
$rho   sqrt($x*$x $y*$y $z*$z);
    
$phi   acos($z/sqrt($x*$x $y*$y $z*$z));
    
$theta $this->arctan($x$y);
    return array(
"rho"=>$rho"phi"=>$phi"theta"=>$theta); 
  }
  
  
/*
  * Transform polar to Cartesian coordinates. 
  */
  
function pol2cart() {
  } 
  
  
/* 
  * Transform spherical to Cartesian coordinates.
  */
  
function sph2cart($rho$theta$phi) {  
    if (
$theta 0) {
      
$x $rho sin($phi) * cos($theta);
      
$y $rho sin($phi) * sin($theta);
      
$z $rho cos($phi);
    }  
    if (
$theta 0) {
      
$x $rho cos($phi) * cos($theta);
      
$y $rho cos($phi) * sin($theta);
      
$z $rho sin($phi);
    }    
    return array(
"x"=>$x"y"=>$y"z"=>$z);
  } 
  
}
?>

Permalink 

Converting between cartesian and spherical coordinates [Space & Physics
Posted on October 12, 2006 @ 02:41:45 AM by Paul Meagher

The function definition below computes the mapping from [x, y, z] -> [rho, phi, theta].

<?php
/*
* @package Math_Function
* @file cartesian2spherical.php
* @function cartesian2spherical
*
* @about Computes the mapping from cartesian to spherical 
* coordinates: {x,y,z} -> {rho, phi, theta} where rho 
* is the radial distance from xyz-origin to the xyz-point, 
* phi is the azimuth angle from the xyz-point to the 
* positive x axis, and theta is the zenith angle from 
* positive z axis to the xyz-point,
*
* @see http://en.wikipedia.org/wiki/Spherical_coordinates
* @see http://www.math.montana.edu/frankw/ccp/multiworld/multipleIVP/spherical/body.htm
* @see http://www.math.uri.edu/~bkaskosz/flashmo/tools/sphcoords/
*
* @param $x real x coordinate
* @param $y real y coordinate
* @param $z real z coordinate
*
* @return array containing $rho, $phi, $theta 
*/ 
function cartesian2spherical($x$y$z) { 
  
$rho   sqrt(($x*$x) + ($y*$y) + ($z*$z));
  
$phi   acos($z/sqrt(($x*$x) + ($y*$y) + ($z*$z)));
  
$theta atan($y/$x);
  return array(
$rho$phi$theta);
}
?>

Below is a simple test script called convert.php that demonstrates usage:

<?php

// include "Math/Function/Trig/cartesian2spherical.php";
include "../cartesian2spherical.php";

$x 1;
$y 2;
$z 3;

$spherical_coords cartesian2spherical($x$y$z);

list(
$rho$phi$theta) = $spherical_coords;

echo 
"xyz = ($x, $y, $z) <br />";
echo 
"rho: $rho <br />";
echo 
"phi: $phi <br />"
echo 
"theta: $theta";

?>

The output of this script is:

xyz = (1, 2, 3)
rho: 3.74165738677
phi: 0.640522312679
theta: 1.10714871779

To learn more about spherical coordinates, consult some of the @see links embedded within the function definition above.

Exercises

1. You might have some fun developing a PHP script that accepts x, y, z input and outputs the corresponding rho, phi, theta coordinates.

2. Develop a spherical2cartesian.php script and include it in the convert.php script to provide two-way transformations.

3. Instead of outputing the rho, phi, and theta coordinates as text values, express them as angles and lines superimposed upon a 3D sphere. SVG would be tool to consider using for your graphing experiments.

Permalink 

 Archive 
 


php/Math Project
© 2011. All rights reserved.