<?php /** * check_domain * * Check a domain name against a whitelist and blacklist. * * @version 0.1 * @author Contributors at eXorithm * @link /algorithm/view/check_domain Listing at eXorithm * @link /algorithm/history/check_domain History at eXorithm * @license /home/show/license * * @param mixed $url * @param array $white_list * @param array $black_list * @return bool */ function check_domain($url='',$white_list=array(0=>'*.gov',1=>'*.gov.ru'),$black_list=array(0=>'*.nk',1=>'*.ru')) { foreach ($white_list as $re) { $re = preg_quote($re); $re = str_replace('\*', '.*', $re); if (preg_match('|^'.$re.'$|', $url)) { return true; } } foreach ($black_list as $re) { $re = preg_quote($re); $re = str_replace('\*', '.*', $re); if (preg_match('|^'.$re.'$|', $url)) { return false; } } return true; } ?>
Category: Algorithm
eXorithm – Execute Algorithm: View / Run Algorithm text_cloud
function text_cloud ($text, $num
{
$words = word_counts$text);
arsort$words);
$words = array_splice$words, 0, $num);
ksort$words);
return tag_cloud$words);
}
eXorithm – Execute Algorithm: View / Run Algorithm testDomain
function testDomain ($url
{
  $result = parse_url$url);
  return $result
}Â
eXorithm – Execute Algorithm: View / Run Algorithm snowflake_fractal
function snowflake_fractal ($length, $level, $color, $image, $x, $y, $orientation
{
if ($image==='') {
// initialize
$image = image_create_alpha$length+2, round$length*4/3*sindeg2rad(60)))+2);
$x = $length+1;
$y = round$length/3*sindeg2rad(60)))+1;
$orientation = -90;
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
$start = true;
} else {
$start = false;
}
if ($level==0) {
// all drawing is done at depth 0
$x2 = ($length)*sindeg2rad$orientation))+$x
$y2 = ($length)*cosdeg2rad$orientation))+$y
imageline$image, round$x), round$y), round$x2), round$y2), $color);
// keep track of the image, current location, and the direction we're facing
return array$image, $x2, $y2, $orientation);
} else {
// draw a side (Koch curve)
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation-60) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation-60) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
// draw the other two sides
if ($start) {
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length, $level, $color, $image, $x, $y, $orientation);
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length, $level, $color, $image, $x, $y, $orientation);
return $image
} else {
// keep track of the image, current location, and the direction we're facing
return array$image, $x, $y, $orientation);
}
}
}
eXorithm – Execute Algorithm: History For Algorithm rotate_image_alpha
rotate_image_alpha    version 0.3    Rotate an image paying respect to transparency.
eXorithm – Execute Algorithm: View / Run Algorithm derivative
function derivative ($function, $with_respect_to
{
  // $with_respect_to is a descriptive variable name, but it's too long!
  $x = $with_respect_to
 Â
  if (is_array$function)) {
     Â
    // pop the operator off the front of the equation
    $operator = array_shift$function);
   Â
    switch ($operator) {
       Â
      case '+'
        // (f+g)' = f' + g'
        return simplify_equationarray'+', derivative$function[0], $x), derivative$function[1], $x)));
 Â
      case '-'
        // (f-g)' = f' - g'
        return simplify_equationarray'-', derivative$function[0], $x), derivative$function[1], $x)));
 Â
      case '*'
        // (f*g)' = f'*g + f*g'
        return simplify_equationarray'+'
                     array'*', derivative$function[0], $x), $function[1]),
                     array'*', $function[0], derivative$function[1], $x))
                    ));
 Â
      case '/'
        // (f/g)' = (f'*g - f*g') / g^2
        return simplify_equationarray'/'
                     array'-'
                           array'*', derivative$function[0], $x), $function[1]),
                           array'*', $function[0], derivative$function[1], $x))
                          ),
                     array'^', $function[1], 2)
                    ));
 Â
      case '^'
        // (f^g)' = (f^g) * (g'*ln(f) + (g/f)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], $function[1]),
                     array'+'
                           array'*'
                                 derivative$function[1], $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', $function[1], $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
       Â
      case 'neg'
        // (-f') = -(f')
        return simplify_equationarray'neg', derivative$function[0], $x)));
   Â
      case 'sqrt'
        // (f^(1/2))' = (f^(1/2)) * ((1/2f)*f')
        return simplify_equationarray'*'
                     array'sqrt', $function[0]),
                     array'*'
                            array'/', 1, array'*', 2, $function[0])),
                            derivative$function[0], $x
                          )
                    ));
 Â
      case 'log'
        // (log(f,g))' = (1/(f*ln(g))) * f'
        return simplify_equationarray'*'
                     array'/', 1, array'*'
                                         $function[0],
                                         array'ln', $function[1])
                                         )),
                     derivative$function[0], $x
                    ));
 Â
      case 'ln'
        // (ln(f))' = (1/f) * f'
        return simplify_equationarray'*'
                     array'/', 1, $function[0]),
                     derivative$function[0], $x
                    ));
 Â
      case 'exp'
        // (exp(f))' = exp(f)
        return simplify_equationarray'exp', $function[0]));
 Â
      case 'root'
        // (f^(1/g))' = (f^(1/g)) * ((1/g)'*ln(f) + (1/fg)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], array'/', 1, $function[1])),
                     array'+'
                           array'*'
                                 derivativearray'/', 1, $function[1]), $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', array'/', 1, $function[1]), $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
       Â
        // trig
        // these all use the chain rule (f(g(x)))' = f'(g(x)) * (g(x))'
       Â
      case 'sin'
        return simplify_equationarray'*'
                     array'cos', $function[0]),
                     derivative$function[0], $x
                    ));
       Â
      case 'cos'
        return simplify_equationarray'*'
                     array'neg', array'sin', $function[0])),
                     derivative$function[0], $x
                    ));
 Â
      case 'tan'
        return simplify_equationarray'*'
                     array'/', 1, array'^', array'cos', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
 Â
      case 'sec'
         return simplify_equationarray'*'
                     array'*'
                           array'sec', $function[0]),
                           array'tan', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
 Â
      case 'csc'
         return simplify_equationarray'*'
                     array'*'
                           array'neg', array'csc', $function[0])),
                           array'cot', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
 Â
      case 'cot'
         return simplify_equationarray'*'
                     array'/', -1, array'^', array'sin', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
       Â
     // below are not done
       Â
        // hyperbolic trig
      case 'sinh'
 Â
      case 'cosh'
 Â
      case 'tanh'
 Â
      case 'sech'
 Â
      case 'csch'
 Â
      case 'coth'
 Â
        // arc trig
      case 'arcsin'
 Â
      case 'arccos'
 Â
      case 'arctan'
 Â
        // inverse hyperbolic trig
      case 'arsinh'
 Â
      case 'arcosh'
 Â
      case 'artanh'
 Â
      default
        throw new Exception'usupported operator in derivative '$operator);
    }
  } else {
    if (ctype_alpha$function)) {
      if ($function==$x) {
        return 1;
      } else {
        throw new Exception'function contains a variable other than '$x'!');
      }
    } else {
      return 0;
    }
  }
  return $function
}Â
eXorithm – Execute Algorithm: View / Run Algorithm random_usort
function random_usort ($a, $b
{
return mt_rand(-1,1);
}
eXorithm – Execute Algorithm: View / Run Algorithm render_polygons
function render_polygons ($polygons, $vertex_color, $face_color, $wireframe, $dashes, $image_size, $scale
{
foreach ($polygons as $polygon) {
if (!is_array$polygon)) {
throw new Exception'Each polygon must be a list.');
} else if ((count$polygon)%3)!=0) {
throw new Exception'Each polygon must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
}
}
if (is_array$vertex_color)) {
if (count$vertex_color) != count$polygons)) {
throw new Exception'If vertex colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
if (is_array$face_color)) {
if (count$face_color) != count$polygons)) {
throw new Exception'If face colors is an array, it must contain the same number of colors as the number of polygons.');
}
}
// if scale=0 then we auto-scale
if ($scale==0) {
$max = 0;
for ($i=0; $icount$polygons); $i++) {
for ($j=0; $jcount$polygons$i]); $j$j+3) {
if (abs$polygons$i][$j])>$max
$max = abs$polygons$i][$j]);
if (abs$polygons$i][$j+1])>$max
$max = abs$polygons$i][$j+1]);
}
}
if ($max>0)
$scale = ($image_size-2)/($max*2);
}
// the polygon arrays (x,y,z) must be converted into shapes (x,y)
$shapes = array();
$z_max = array();
for ($i=0; $icount$polygons); $i++) {
$max = $polygons$i][2];
for ($j=0; $jcount$polygons$i]); $j$j+3) {
$x = $polygons$i][$j];
$y = $polygons$i][$j+1];
// map each x,y coord to a screen position
$x = round$image_size/2 + $x$scale);
$y = round$image_size/2 - $y$scale);
$shapes$i][$j] = $x
$shapes$i][$j+1] = $y
// keep track of the maximum z-value for each shape
if ($polygons$i][$j+2]>$max
$max = $polygons$i][$j+2];
}
$shapes$i] = array_values$shapes$i]);
$z_max$i] = $max
}
// create a blank image
$image = image_create_alpha$image_size, $image_size);
// create the colors
if (!is_array$vertex_color))
$vertex_color = array_fill(0, count$polygons), $vertex_color);
if (!is_array$face_color))
$face_color = array_fill(0, count$polygons), $face_color);
// painter's algorithm - draw farther polygons first
array_multisort$z_max, SORT_DESC, $shapes, $face_color, $vertex_color);
// draw the polygons
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$f_color = allocate_color$image, $face_color$i]);
if (!$wireframe) {
imagefilledpolygon$image, $shapes$i], count$shapes$i])/2, $f_color);
}
imagepolygon$image, $shapes$i], count$shapes$i])/2, $v_color);
}
// draw dashes - BUGGY
if ($dashes) {
for ($i=0; $icount$shapes); $i++) {
$v_color = allocate_color$image, $vertex_color$i]);
$style = array$v_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle$image, $style);
imagepolygon$image, $shapes$i], count$shapes$i])/2, IMG_COLOR_STYLED);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm convert2base
function convert2base ($num, $base
{
$symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'
if (($base<2) || ($basestrlen$symbols)))
throw new Exception'Base must be between 2 and 'strlen$symbols));
$result = ''
while ($num > 0) {
$result = $symbols$num$base].$result
$num = floor$num$base);
}
return $result
}
eXorithm – Execute Algorithm: View / Run Algorithm prime_factors
function prime_factors ($number
{
$factors = array();
while$number > 1)
{
$prime = 2;
while$number % $prime != 0)
{
$prime = next_prime$prime);
}
if$prime > floor$number/2))
{
$factors[] = $number
break
}
$factors[] = $prime
$number = $number$prime
}
return $factors
}