Advertisement
mate2code

Nimber multiplication algorithm in Matlab

Feb 25th, 2013
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.96 KB | None | 0 0
  1. function y = nimprod(x1,x2)
  2. % nimber multiplication of bin objects  ( for the bin class see http://pastebin.com/K02wGsae )
  3.  
  4.  
  5.  
  6. if x1==bin('0')  ||  x2==bin('0')
  7. % if x1 or x2 is 0
  8.    
  9.     y = bin('0') ;
  10.    
  11. elseif x1==bin('1')  ||  x2==bin('1')
  12. % if x1 or x2 is 1
  13.    
  14.     y = x1*x2 ;
  15.  
  16. elseif length(x1.Expo)==1 && length(x2.Expo)==1 && ~mod( log2(double(x1.Expo)) ,1) && ~mod( log2(double(x2.Expo)) ,1)    
  17. % if x1 and x2 are Fermat powers of 2
  18.    
  19.     if   x1 ~= x2
  20.          y = x1*x2 ;  % Nim product of distinct Fermat powers of 2 is the normal product.
  21.     else
  22.          product = bin('11') * x1 ;     % Nim product of equal Fermat powers of 2 is 3/2 * entry.
  23.          y = bin( product.Expo - 1 ) ;  % Division by two is rightshift.
  24.     end
  25.  
  26. elseif length(x1.Expo)==1 && length(x2.Expo)==1
  27. % if x1 and x2 are powers of 2 (although at least one is not a Fermat power of 2)
  28.  
  29.     % The Expo of powers of 2 is a single number, which now has to be converted to a bin object.
  30.     e1 = bin( x1.Expo ,'dec') ;                  
  31.     e2 = bin( x2.Expo ,'dec') ;                  
  32.     xor = bitxor(e1,e2) ;
  33.     and = bitand(e1,e2) ;
  34.  
  35.     XOR = bin('1') ;
  36.     for m = 1 : xor.Weight
  37.         XOR = XOR * bin( 2^(xor.Expo(m)) ) ;
  38.     end
  39.  
  40.     if and.Weight==1
  41.         % AND = 3/2 * bin( 2^(and.Expo) )
  42.         product = bin('11') * bin( 2^(and.Expo) ) ;
  43.         AND = bin( product.Expo - 1 ) ;  % Division by two is rightshift.
  44.     else
  45.         AND = bin('1') ;
  46.         for m = 1 : and.Weight
  47.             % AND = nimprod(   AND   ,   3/2 * bin( 2^(and.Expo(m)) )   )
  48.             product = bin('11') * bin( 2^(and.Expo(m)) ) ;
  49.             factor = bin( product.Expo - 1 ) ;  % Division by two is rightshift.
  50.             AND = nimprod( AND , factor ) ;
  51.         end
  52.     end
  53.  
  54.     y = nimprod( XOR , AND ) ;
  55.  
  56. else  % if x1 or x2 is not a power of 2.
  57.  
  58.     Y = bin('0') ;
  59.    
  60.     for m = 1 : x1.Weight
  61.         for n = 1 : x2.Weight
  62.             M = x1.Expo(m) ;  N = x2.Expo(n) ;
  63.             if M==0 || N==0                     % if bin(M)=1 or bin(N)=1 ,  i.e. if M=0 or N=0
  64.                 Y = bitxor( Y ,   bin(M)*bin(N)   ) ;
  65.             elseif ~mod( log2(double(M)) ,1) && ~mod( log2(double(N)) ,1)  % if bin(M) and bin(N) are Fermat powers of 2 ,  i.e. if M and N are powers of 2
  66.                 if M ~= N
  67.                     Y = bitxor( Y ,   bin(M)*bin(N)   ) ;             % Nim product of distinct Fermat powers of 2 is the normal product.
  68.                 else
  69.                     product = bin('11') * bin(M) ;                    % Nim product of   equal  Fermat powers of 2 is 3/2 * entry.
  70.                     Y = bitxor( Y ,   bin( product.Expo - 1 )   ) ;   % Division by two is rightshift.
  71.                 end
  72.             else
  73.                 binM = bin(M) ;
  74.                 binN = bin(N) ;
  75.                 Y = bitxor( Y ,     nimprod(  bin(binM.Expo)  ,  bin(binN.Expo)  )     ) ;
  76.             end
  77.         end
  78.     end
  79.  
  80.     y = Y ;
  81.    
  82. end
  83.  
  84.  
  85.  
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement