Tag Archives: PHP

Cantor Tuple Function

The Cantor Tuple Function, inductively defined as cantor_tuple_function is a generalization of the Cantor Pairing Function cantor_pairing_function

The Cantor Tuple Function can easily be calculated using the following algorithm:

function cantor( list ) {

    // Empty list?
    if list is empty return;
    
    // One element list
    if list.length == 1 return list.firstElement

    // Multiple element list -> Pop last element from list
    lastElement = list.popLastElement

    return ( 0.5 * ( cantor( list ) + lastElement  ) * ( cantor( list ) + lastElement + 1 ) + lastElement );

} // cantor()

The following code is a PHP implementation of this algorithm using the BCMath extension for Arbitrary Precision Mathematics:

/**
 * Calculates the Cantor tupel function 
 * for variable length arguments
 *
 * @param <array(x1,..xk)> k-Tupel
 * @return <integer> Value of the Cantor tupel function
 * @author code-kobold (Ron Metten) (www.code-kobold.de)
 * @since 2009 Okt 29
 */
function cantor( $list ) {

    // empty list?
    if( count( $list ) == 0 ) {
        print "Empty list\n";
        return;
	}
    
    // one element list
    if( count( $list ) == 1 ) {
        return $list[0];        
    }

    // More than one element list: 
    // Pop last list element, list rest remains in $list
    $lastElement = array_pop( $list );
        
    return ( 
	bcadd( 
		bcmul( '0.5', 
			bcmul(( 
				bcadd( cantor( $list), $lastElement)  ), ( 
					bcadd( cantor($list), 
						bcadd( $lastElement, 1 ) ) )) ), 
		$lastElement ));

} // cantor()

Usage:

$tupel = array( 111,222,333,444,555,666,777 );

print "\n";

print 'Calculating Cantor tupel ( ';

foreach( $tupel as $t ) {
	print $t . " ";
}
print ') = '. cantor( $tupel );

returns:

Calculating Cantor tupel ( 111 222 333 444 555 666 777 ) = 
44807337240164294928814443042660513844953064441167412950347414572411004987014641183772758578219498576426251586564328579489775691797120860568962

Drawing arrows with the GD library in PHP

While generating schemes for finite state machines with PHP, I missed the possibility to draw arrows with aligned arrowheads using the GD library. The presented class GDArrow draws such arrows in a referenced canvas. The usage is easy:

* $myArrow = new GDArrow()
*
* $myArrow -> image = foo
* $myArrow -> color = bar
* $myArrow -> x1 = foo
* $myArrow -> y1 = bar
* $myArrow -> x2 = foo
* $myArrow -> y2 = bar
* $myArrow -> angle = foo
* $myArrow -> radius = bar
*
* $myArrow -> drawGDArrow()

x1y1 is the starting point, x2y2 the endpoint of the arrow, angle is the angle of the arrowheads arm segments to the arrowline, radius the length of these segments.

The class can easily be included, the member variables are public. Download here

The following image was created using the GDArrow class:

gdarrow

Different folders for sources and deployment

When developing PHP using Netbeans 6.5.1, you probably don’t want to store your projects source files in your web server root, e.g. for version control.

You can choose a different location for your sources and deploy your application in the web server root.

Right cklick your project, choose properties, select Copy files from Sources Folder to another location. Under Copy to folder choose your server root, a folder with the project name will be generated.

Customizing a Netbeans Template

Customizing the Netbeans 5.6.1 PHP Template is easy.

Under Tools/Templates you find the Template Manager. There you can customize the templates for PHP classes and interfaces. In my case I prefer the date given in the PHPDocs in the following format: “2009 Jun 25″. The ${date} variable is a String. Casting this into date type, the output can be formatted according to java.text.SimpleDateFormat, in my case ${date?date?string(“yyyy MMM d”)}.

To change the user name, edit User.properties under Tools/Templates/User Configuration Properties:

user=Code-Kobold (Ron Metten) (www.code-kobold.de)

The template now looks like this:

<?php
/**
 * Description of ${name}
 *
 * @author ${user}
 * @since ${date?date?string("yyyy MMM d")}
 */
 class ${name} {
 } // class ${name}
?>

This generates the following output, when a new PHP Class is created:

<?php
/**
 * Description of Template
 *
 * @author Code-Kobold (Ron Metten) (www.code-kobold.de)
 * @since 2009 Jun 26
 */
 class Template {
 } // class Template
?>