lib/rationalnestedset.js
function calculate_encoding_from_tree_position(position_array){

  // if we have only one position then it means we have a root element (i.e. one at the top of the tree)
  // and this is an easy fraction to calculate (i.e. x / 1)
  if(position_array.length==1){
    return get_parsed_encodings({
    
      // the numerator is the position of the root element
      numerator:position_array[0],
      
      // the denominator is always 1
      denominator:1
      
    });
  }
  
  // initialize the data array we will work with
  var data = {
    parts:_.map(position_array, function(v){return v;}),
    unityparts:[]
  };
  
  // lets insert the alternate unity into the position array
  for(var i=0; i<data.parts.length; i++)
  {   
    // add the next element into the array
    // and as long as we are not the last element, insert unity
    
    data.unityparts.push(data.parts[i]);

    if(i<data.parts.length-1)
    {
      data.unityparts.push(1);
    }
  }
  
  // There will always be AT LEAST 3 elements in the array at this point
  // initialize values by purging the last element from the array as the initial numerator
  // the denominator will always start off as null because the first fraction will be just the last_element (i.e. last_element / 1)
  data.numerator = data.unityparts.pop();
  data.denominator = 1;
  
  // the integer starts off as 0 to deal with the not-possible event of if we only had 1 or 2 array elements
  data.integer = 0;
  
  // now lets start reducing the continued fraction!
  while(data.unityparts.length>0)
  {
    data.integer = data.unityparts.pop();

    data = reduce_continued_fraction_step(data);

    // this has just performed:
    // a,b -> a + 1 / b
  }
  
  return get_parsed_encodings(data);
}
function calculate_encoding_from_tree_position(position_array){

  // if we have only one position then it means we have a root element (i.e. one at the top of the tree)
  // and this is an easy fraction to calculate (i.e. x / 1)
  if(position_array.length==1){
    return get_parsed_encodings({
    
      // the numerator is the position of the root element
      numerator:position_array[0],
      
      // the denominator is always 1
      denominator:1
      
    });
  }
  
  // initialize the data array we will work with
  var data = {
    parts:_.map(position_array, function(v){return v;}),
    unityparts:[]
  };
  
  // lets insert the alternate unity into the position array
  for(var i=0; i<data.parts.length; i++)
  {   
    // add the next element into the array
    // and as long as we are not the last element, insert unity
    
    data.unityparts.push(data.parts[i]);

    if(i<data.parts.length-1)
    {
      data.unityparts.push(1);
    }
  }
  
  // There will always be AT LEAST 3 elements in the array at this point
  // initialize values by purging the last element from the array as the initial numerator
  // the denominator will always start off as null because the first fraction will be just the last_element (i.e. last_element / 1)
  data.numerator = data.unityparts.pop();
  data.denominator = 1;
  
  // the integer starts off as 0 to deal with the not-possible event of if we only had 1 or 2 array elements
  data.integer = 0;
  
  // now lets start reducing the continued fraction!
  while(data.unityparts.length>0)
  {
    data.integer = data.unityparts.pop();

    data = reduce_continued_fraction_step(data);

    // this has just performed:
    // a,b -> a + 1 / b
  }
  
  return get_parsed_encodings(data);
}
function calculate_encoding_from_tree_position ( position_array ) { // if we have only one position then it means we have a root element (i.e. one at the top of the tree) // and this is an easy fraction to calculate (i.e. x / 1) if ( position_array . length == 1 ) { return get_parsed_encodings ( { // the numerator is the position of the root element numerator : position_array [ 0 ] , // the denominator is always 1 denominator : 1 } ) ; } // initialize the data array we will work with var data = { parts : _ . map ( position_array , function ( v ) { return v ; } ) , unityparts : [ ] } ; // lets insert the alternate unity into the position array for ( var i = 0 ; i < data . parts . length ; i ++ ) { // add the next element into the array // and as long as we are not the last element, insert unity data . unityparts . push ( data . parts [ i ] ) ; if ( i < data . parts . length - 1 ) { data . unityparts . push ( 1 ) ; } } // There will always be AT LEAST 3 elements in the array at this point // initialize values by purging the last element from the array as the initial numerator // the denominator will always start off as null because the first fraction will be just the last_element (i.e. last_element / 1) data . numerator = data . unityparts . pop ( ) ; data . denominator = 1 ; // the integer starts off as 0 to deal with the not-possible event of if we only had 1 or 2 array elements data . integer = 0 ; // now lets start reducing the continued fraction! while ( data . unityparts . length > 0 ) { data . integer = data . unityparts . pop ( ) ; data = reduce_continued_fraction_step ( data ) ; // this has just performed: // a,b -> a + 1 / b } return get_parsed_encodings ( data ) ; }
Calculates a finite continued fraction encoding of tree position returning a numerator and denominator representing the encoding

The tree position is provided as an array of positions, so for example the 3rd element of the 7th element of the 2nd element is represented as:

// returns array('n' => 65, 'd' => 23)
// passed array represents root -> 2nd node -> 4th node -> 3rd node
//
Tree_Nestedsetencoder::calculate_encoding_from_tree_position(array(2, 4, 3));

This will return a numerator and denominator in an object that can be saved to the database

It reduces a continued fraction after alternating unity into the position array

@access public
@param array $tree_position_array (default: array()) an array of the numeric tree positions for the node to calculate an encoding for
@return object and object containing 'numerator', 'denominator' and 'rational' string properties
Calculates a finite continued fraction encoding of tree position returning a numerator and denominator representing the encoding