Extension:MathFunctions
From MediaWiki.org
|
MathFunctions Release status: experimental |
|
|---|---|
| Implementation | Parser function |
| Description | Enhances parser with mathematical functions |
| Author(s) | Matěj Grabovský (DJ Jeri Talk) |
| Version | 0.5 (March 9, 2008) |
| MediaWiki | >= 1.11.0 |
| Download | see below |
| Hooks used | |
Contents |
[edit] What can this extension do?
MathFunctions enhances MediaWiki parser with mathematical functions sin, cos, tan, sqrt and others. This extension also adds ability to convert numbers between numerical bases and ability convert radians to deggress and reverse.
[edit] Usage
Just use as normal parser functions extensions. E.g. {{#asin:514}} or {{#log:2513|11}}.
[edit] Download instructions
Just copy the code from code section and paste it to a file, that save to your extensions directory as MathFunctions.php.
[edit] Installation
To install this extension, add the following at the end (but before closing delimiter ?>) of your LocalSettings.php:
# MathFunctions extension require_once( "$IP/extensions/MathFunctions.php" );
[edit] Code
<?php $wgExtensionCredits['parserhook'][] = array( 'name' => "MathFunctions", 'description' => "Enhances parser with mathematical functions", 'version' => "$wgMathFunctionsVersion - $wgMathFunctionsVersionDate", 'author' => "Matěj Grabovský ([[:mw:User:DJ Jeri|DJ Jeri]])", 'url' => "http://www.mediawiki.org/wiki/Extension:MathFunctions", ); $wgExtensionFunctions[] = 'wfMathFunctions_Setup'; $wgHooks['LanguageGetMagic'][] = 'wfMathFunctions_Magic'; function wfMathFunctions_Setup() { global $wgParser; $wgParser->setFunctionHook( 'sin', 'efMathFunctionsSin_Render' ); $wgParser->setFunctionHook( 'asin', 'efMathFunctionsAsin_Render' ); $wgParser->setFunctionHook( 'cos', 'efMathFunctionsCos_Render' ); $wgParser->setFunctionHook( 'acos', 'efMathFunctionsAcos_Render' ); $wgParser->setFunctionHook( 'tan', 'efMathFunctionsTan_Render' ); $wgParser->setFunctionHook( 'atan', 'efMathFunctionsAtan_Render' ); $wgParser->setFunctionHook( 'atan2', 'efMathFunctionsAtan2_Render' ); $wgParser->setFunctionHook( 'pow', 'efMathFunctionsPow_Render' ); $wgParser->setFunctionHook( 'log', 'efMathFunctionsLog_Render' ); $wgParser->setFunctionHook( 'sqrt', 'efMathFunctionsSqrt_Render' ); $wgParser->setFunctionHook( 'deg2rad', 'efMathFunctionsDeg2rad_Render' ); $wgParser->setFunctionHook( 'rad2deg', 'efMathFunctionsRad2deg_Render' ); $wgParser->setFunctionHook( 'baseconvert', 'efMathFunctionsBaseconvert_Render' ); global $wgMathFunctionsVersion, $wgMathFunctionsVersionDate; $wgMathFunctionsVersion = "0.5"; $wgMathFunctionsVersionDate = "March 9, 2008"; } function wfMathFunctions_Magic( &$magicWords, $langCode ) { switch ( $langCode ) { default: $magicWords['sin'] = array( 0, 'sin' ); $magicWords['asin'] = array( 0, 'asin' ); $magicWords['cos'] = array( 0, 'cos' ); $magicWords['acos'] = array( 0, 'acos' ); $magicWords['tan'] = array( 0, 'tan' ); $magicWords['atan'] = array( 0, 'atan' ); $magicWords['atan2'] = array( 0, 'atan2' ); $magicWords['pow'] = array( 0, 'pow' ); $magicWords['log'] = array( 0, 'log' ); $magicWords['sqrt'] = array( 0, 'sqrt' ); $magicWords['deg2rad'] = array( 0, 'deg2rad' ); $magicWords['rad2deg'] = array( 0, 'rad2deg' ); $magicWords['baseconvert'] = array( 0, 'baseconvert' ); } return true; } function efMathFunctionsSin_Render( &$parser, $n ){ return sin( $n ); } function efMathFunctionsAsin_Render( &$parser, $n ){ return asin( $n ); } function efMathFunctionsCos_Render( &$parser, $n ) { return cos( $n ); } function efMathFunctionsAcos_Render( &$parser, $n ) { return acos( $n ); } function efMathFunctionsTan_Render( &$parser, $n ) { return tan( $n ); } function efMathFunctionsAtan_Render( &$parser, $n ) { return atan( $n ); } function efMathFunctionsAtan2_Render( &$parser, $x, $y ) { return atan2( $x, $y ); } function efMathFunctionsPow_Render( &$parser, $base, $exp = 2 ) { return pow( $base, $exp ); } function efMathFunctionsLog_Render( &$parser, $n, $base = 10 ) { return log( $n, $base ); } function efMathFunctionsSqrt_Render( &$parser, $n ) { return sqrt( $n ); } function efMathFunctionsDeg2rad_Render( &$parser, $n ) { return deg2rad( $n ); } function efMathFunctionsRad2deg_Render( &$parser, $n ) { return rad2deg( $n ); } function efMathFunctionsBaseconvert_Render( &$parser, $n, $from, $to ) { return base_convert( $n, $from, $to ); }

