% Installer: "bigint_install.m"
% Created: 30-Jul-2002 16:16:36.

function bund_driver

% bund_driver -- Driver for "bund" bundles.
%  bund_driver (no arguments) contains Matlab commands
%   to inflate the instructions and files that are
%   encoded into the "bund_data" function of this package.
 
% Copyright (C) 2001 Dr. Charles R. Denham, ZYDECO.
%  All Rights Reserved.
%   Disclosure without explicit written consent from the
%    copyright owner does not constitute publication.
 
% Version of 14-Jun-2001 10:54:16.
% Updated    03-Aug-2001 13:43:17.

help(mfilename)

BINARY_TAG = '?';

CR = char(13);
LF = char(10);

comp = upper(computer);
if any(findstr(comp, 'PCWIN'))   % Windows.
	NL = [CR LF];
elseif any(findstr(comp, 'MAC2'))   % Macintosh.
	NL = CR;
else   % Unix.
	NL = LF;
end

c = zeros(1, 256);
c(abs('0'):abs('9')) = 0:9;
c(abs('a'):abs('f')) = 10:15;

disp([' '])
disp([' ## This installer is ready to expand its contents,'])
disp([' ## starting in the present directory: "' pwd '"'])
disp([' ## To abort, execute an interruption now.'])
disp([' ## Otherwise, to continue, press any key.'])
disp([' '])

try
	pause
catch
	disp([' ## Installation interrupted.'])
	disp([' '])
	return
end

tic

w = which(mfilename);

fin = fopen(w, 'r');
if fin < 0, return, end

found = ~~0;
while ~found
	s = fgetl(fin);
	if isequal(s, -1)
		fclose(fin);
		return
	end
	s = strrep(s, LF, '');  % Not necessary?
	s = strrep(s, CR, '');  % Not necessary?
	if isequal(s, 'function bund_data')
		found = ~~1;
	end
end

fout = -1;

done = ~~0;
while ~done
	s = fgetl(fin);
	if isequal(s, -1)
		fclose(fin);
		return
	end
	if length(s) > 0
		if s(1) ~= '%'
			f = findstr(s, 'bund_setdir');
			if any(f)
				theDir = eval(strrep(s, 'bund_setdir', ''));
				status = mkdir(theDir);
				switch status
				case 1
					disp([' ## Directory created: "' theDir '"'])
				case 2
					disp([' ## Directory exists: "' theDir '"'])
				otherwise
					error([' ## Error while making new directory.'])
				end
				
				try
					cd(theDir)
				catch
					error([' ## Unable to go to directory: "' theDir '"'])
				end
			else
				try
					eval(s);
				catch
					error([' ## Unable to evaluate: "' s '"'])
				end
			end
		elseif length(s) > 1 & s(2) == BINARY_TAG
			hx = double(s(3:end));   % Assume hex data.
			bin = 16*c(hx(1:2:end)) + c(hx(2:2:end));
			fwrite(fout, bin, 'uchar');
		else
			fprintf(fout, '%s', s(2:end));
			fprintf(fout, NL);
		end
	end
end

fclose(fin);

disp([' ## Elapsed time: ' num2str(fix(10*toc)/10) ' s.'])

function bund_data
bund_setdir('bigint')

disp(' ## Installing: "bigint_bundle.m" (text)')
fout = fopen('bigint_bundle.m', 'w');
%function bigint_bundle
%
%% bigint_bundle -- Bundle the BigInt Toolbox.
% 
%% Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Apr-1999 09:11:21.
%% Updated    30-Jul-2002 14:31:04.
%
%at(mfilename)
%
%thePackage = 'bigint';
%
%theClasses = {
%	thePackage
%};
%
%for i = 1:length(theClasses)
%	newversion(theClasses{i})
%end
%
%theMFiles = {
%	mfilename
%};
%
%theMFiles = sort(theMFiles);
%
%theMessage = {
%	' '
%	[' ## Place "' thePackage '" in your Matlab path,']
%	[' ## then restart Matlab.  Execute "' thePackage ' demo"']
%	' ## at the Matlab prompt.'
%	' '
%};
%
%bund('new', thePackage)
%bund('setdir', thePackage)
%bund('mfile', theMFiles)
%bund('class', theClasses)
%bund cd ..
%bund('disp', theMessage)
%bund close
fclose(fout);
bund_setdir('@bigint')

disp(' ## Installing: "abs.m" (text)')
fout = fopen('abs.m', 'w');
%function theResult = abs(self)
%
%% bigint/abs -- Square-root of a "bigint" object.
%%  abs(self) returns the absolute value of self,
%%   a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 16:49:24.
%
%if nargin < 1, help(mfilename), return, end
%
%result = sign(self, +1);
%
%if nargout > 0
%    theResult = result;
%else
%    assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "and.m" (text)')
fout = fopen('and.m', 'w');
%function theResult = and(self, other)
%
%% bigint/and -- Logical-AND of two "bigint" objects.
%%  and(self, other) returns (self & other), for
%%   the given objects, one of which must be a "bigint".
%%
%% Also see: or, not.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%result = (abs(self) ~= 0 & abs(other) ~= 0);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "base.m" (text)')
fout = fopen('base.m', 'w');
%function theResult = base(self)
%
%% bigint/base -- Base of "bigint" (always 1000).
%%  base(self) returns the base of self, a "bigint"
%%   object.  The base is always 1000.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 21:53:06.
%
%if nargin < 1, help(mfilename), return, end
%
%result = 1000;
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "bigint.m" (text)')
fout = fopen('bigint.m', 'w');
%function self = bigint(varargin)
%
%% bigint/bigint -- Constructor for "bigint" class.
%%  bigint(theValue) constructs a 'bigint" object from
%%   theValue, a vector of signed base-1000 coefficients,
%%   or a string of decimal digits, possibly signed and
%%   comma-delimited.  A "bigint" is a large integer,
%%   suitable for representing exact integers that are
%%   too large for double-precision representation.
%%   They respond to the following operators:
%%            + - * / ^ < <= == >= > ~= | & ~
%%   Other functions are available; see "methods bigint"
%%   for the complete list.  Some functions, such as
%%   division and "sqrt", are computed via bisection.
%%  bigint(...) returns a "bigint" constructed from
%%   the left-to-right concatenation of the arguments.
%%   For a negative-number, use non-negative arguments,
%%   then change the "bigint" sign with unary-minus.
%%  bigint(aBigInt) returns aBigInt intact.
%%  bigint([]) returns bigint(0).
%%  bigint('demo') demonstrates itself.
%%  bigint (no argument) returns bigint(0).
%%
%% See also: methods('bigint').
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 12:37:20.
%% Updated    30-Jul-2002 15:53:49.
%
%if nargin < 1 & nargout < 1
%	eval('setdef(mfilename)', '')
%	return
%end
%
%if nargin < 1, varargin = {0}; end
%
%if isequal(varargin{1}, 'demo')
%	help(mfilename)
%	x = bigint(123,456);   % Same as bigint('123,456').
%	y = bigint(789);       % Same as bigint('789').
%    sum = x + y;
%    difference = x - y;
%	product = x * y;
%	quotient = x / y;
%    remainder = rem(x, y);
%	greatest_common_divisor = gcd(x, y);
%	least_common_multiple = lcm(x, y);
%    y_squared = y^2;
%    y_cubed = y^3;
%    two_to_the_y_power = 2^y;
%	square_root_of_y = sqrt(y);
%	cube_root_of_y = root(y, 3);
%    log_base_two_of_y = log2(y);
%	[recip_of_y, scale_factor] = recip(y);
%	one_googal = bigint(10)^100;
%	disp(display(x))
%	disp(display(y))
%	disp(display(sum))
%	disp(display(difference))
%	disp(display(product))
%	disp(display(quotient))
%	disp(display(remainder))
%	disp(display(greatest_common_divisor))
%	disp(display(least_common_multiple))
%	disp(display(y_squared))
%	disp(display(y_cubed))
%	disp(display(two_to_the_y_power))
%	disp(display(square_root_of_y))
%	disp(display(cube_root_of_y))
%	disp(display(log_base_two_of_y))
%	disp([display(recip_of_y) ' / ' disp(scale_factor)])
%	disp(display(one_googal))
%	return
%end
%
%if isa(varargin{1}, 'bigint')   % Pass-through.
%    result = varargin{1};
%elseif nargin == 1 & isempty(varargin{1})   % Same as bigint(0).
%    result = bigint(0);
%else   % Concatenate the arguments.
%    if isa(varargin{1}, 'double')
%        theSign = +1;
%        theValue = [];
%        for i = 1:prod(size(varargin))
%            v = varargin{i};
%            v = v(:)';
%            theValue = [theValue v];
%        end
%    elseif isa(varargin{1}, 'char')
%        theValue = '';
%        for i = 1:length(varargin)
%            theValue = [theValue varargin{i}];
%        end
%        theSign = 1 - 2*any(theValue == '-');
%        theValue = strrep(theValue, '-', '');
%        theValue = strrep(theValue, ',', '');
%        while any(rem(length(theValue), 3))
%            theValue = ['0' theValue];
%        end
%        v = zeros(1, length(theValue)/3);
%        k = 0;
%        for i = 1:3:length(theValue)
%            k = k+1;
%            v(k) = eval(theValue(i:i+2));
%        end
%        theValue = v;
%    end
%% Define an empty structure for the private data.
%    theStruct.itsMagnitude = [];
%    theStruct.itsSign = [];
%% Allocate the object via "class".
%    result = class(theStruct, 'bigint');
%% Initialize the private data via class methods.
%    result = magnitude(result, theValue);
%    result = sign(result, theSign);
%    result = trim(result);
%end
%
%% Return.
%
%if nargout > 0
%    self = result;
%else
%    assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "digits.m" (text)')
fout = fopen('digits.m', 'w');
%function theResult = digits(self)
%
%% bigint/digits -- Number of base-10 digits in a "bigint".
%%  digits(self) returns the number of digits in the base-10
%%   representation of self, a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 23-Feb-1998 11:32:17.
%
%if nargin < 1, help(mfilename), return, end
%
%a = magnitude(trim(self));
%result = bigint(length(int2str(a(1))) + 3*(length(a)-1));
%
%if nargout > 0
%    theResult = result;
%else
%    assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "disp.m" (text)')
fout = fopen('disp.m', 'w');
%function theResult = disp(self)
%
%% bigint/disp -- Display a "bigint" object.
%%  disp(self) displays or returns the value of self
%%   as text, for self a "bigint" object.
%%
%% Also see: display.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:10:01.
%
%if nargin < 1, help(mfilename), return, end
%
%if prod(size(self)) > 1
%    for i = 1:prod(size(self))
%        disp(self(i))
%    end
%    return
%end
%
%self = trim(self);
%theMagnitude = magnitude(self);
%theSign = sign(self);
%s = '';
%
%if theSign < 0
%    s = '-';
%else
%    s = '+';
%end
%
%for i = 1:length(theMagnitude)
%    if i > 1, s = [s, ',']; end
%    t = num2str(theMagnitude(i));
%    if i > 1
%        while length(t) < 3, t = ['0' t]; end
%    end
%    s = [s t];
%end
%
%if nargout > 0
%	theResult = s;
%else
%	disp(' ')
%	disp(s)
%end
fclose(fout);

disp(' ## Installing: "display.m" (text)')
fout = fopen('display.m', 'w');
%function theResult = display(self)
%
%% bigint/display -- Display a "bigint" object.
%%  display(self) displays or returns the value of self
%%   as text, for self a "bigint" object.
%%
%% Also see: display.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:10:01.
%
%if nargin < 1, help(mfilename), return, end
%
%if nargout > 0
%	theResult = [inputname(1) ' = ' disp(self)];
%else
%	disp(' ')
%	disp([inputname(1) ' ='])
%	disp(self)
%end
fclose(fout);

disp(' ## Installing: "div.m" (text)')
fout = fopen('div.m', 'w');
%function [theResult, theScaleFactor] = div(self, other, nDigits)
%
%% bigint/div -- Divide two bigints via reciprocal.
%%  [result, scale_factor] = div(self, other) multiplies
%%   self by the reciprocal of other.  The scale_factor
%%   for the reciprocal is also returned, such that the
%%   product (self*result) equals the scale_factor,
%%   approximately.
%%  div(self, other, nDigits) specifies the precision
%%   of the reciprocal to be used.
% 
%% Copyright (C) 2002 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 30-Jul-2002 16:04:05.
%% Updated    30-Jul-2002 16:04:05.
%
%if nargin < 1, help(mfilename), return, end
%
%if nargin > 2
%	[theReciprocal, theScaleFactor] = recip(other, nDigits);
%else
%	[theReciprocal, theScaleFactor] = recip(other);
%end
%
%result = self * theReciprocal;
%
%% Output.
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "double.m" (text)')
fout = fopen('double.m', 'w');
%function theResult = double(self)
%
%% bigint/double -- Double-equivalent of a "bigint".
%%  double(self) returns the double-precision equivalent
%%   of self.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 18:41:34.
%
%if nargin < 1, help(mfilename), return, end
%
%result = sign(self) * polyval(magnitude(self), 1000);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "eq.m" (text)')
fout = fopen('eq.m', 'w');
%function theResult = eq(self, other)
%
%% bigint/le -- Equal-to comparison for "bigint" objects.
%%  le(self, other) returns true if self <= other, for the
%%   two items, one of which must be a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = ~(self < other) & ~(self > other);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "gcd.m" (text)')
fout = fopen('gcd.m', 'w');
%function theResult = gcd(self, other, verbose)
%
%% bigint/gcd -- Greatest-common-divisor of "bigint" objects.
%%  gcd(self, other) returns the greatest-common-divisor
%%   between self and other, one of which must be a "bigint".
%%
%% Also see: lcm.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 23:52:25.
%
%if nargin < 2, help(mfilename), return, end
%if nargin < 3, verbose = 0; end
%
%a = abs(bigint(self));
%b = abs(bigint(other));
%
%result = 1;
%
%if a ~= 0 & b ~= 0
%    while (1)
%    	r = rem(a, b);
%    	if r < 2, break, end
%if (verbose)
%	disp([' ## ' mfilename ' remainder = ' disp(r)])
%end
%    	a = b;
%    	b = r;
%    end
%end
%
%if r == 0
%    result = b;
%else
%    result = 1;
%end
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "ge.m" (text)')
fout = fopen('ge.m', 'w');
%function theResult = ge(self, other)
%
%% bigint/ge -- Greater-than-or-equal-to for "bigint" objects.
%%  ge(self, other) returns true if self >= other, for the
%%   two items, one of which must be a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = ~(self < other);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "gt.m" (text)')
fout = fopen('gt.m', 'w');
%function theResult = gt(self, other)
%
%% bigint/gt -- Greater-than comparison of "bigint" objects.
%%  gt(self, other) returns true if self > other, for the
%%   two items, one of which must be a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = (other < self);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "half.m" (text)')
fout = fopen('half.m', 'w');
%function theResult = half(self)
%
%% bigint/half -- half of a "bigint".
%%  half(self) returns (self / 2).
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 08-Jul-1998 17:23:07.
%
%if nargin < 1, help(mfilename), return, end
%
%% Use the magnitude.
%
%a = magnitude(self);
%len = length(a);
%
%% Borrow the odd digits.
%
%r = rem(a, 2);
%a = a - r;
%a = a + base(self) .* [0 r(1:len-1)];
%
%% Halve.
%
%result = magnitude(self, a ./ 2);
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "lcm.m" (text)')
fout = fopen('lcm.m', 'w');
%function theResult = lcm(self, other)
%
%% bigint/lcm -- Least-common-multiple of "bigint" objects.
%%  lcm(self, other) returns the least-common-multiple
%%   between self and other, one of which must be a "bigint",
%%   and both of which must be positive numbers.  The least-
%%   common-multiple is self*other/gcd(self, other).
%%
%% Also see: gcd.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 23:52:25.
%
%if nargin < 2, help(mfilename), return, end
%
%if self < 1 | other < 1
%	error(' ## Both arguments must be positive.')
%end
%
%c = gcd(self, other);
%
%result = (self * other) / c;
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "le.m" (text)')
fout = fopen('le.m', 'w');
%function theResult = le(self, other)
%
%% bigint/le -- Less-than-or-equal-to for "bigint" objects.
%%  le(self, other) returns true if self <= other, for the
%%   two items, one of which must be a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = ~(self > other);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "log10.m" (text)')
fout = fopen('log10.m', 'w');
%function theResult = log10(self)
%
%% bigint/log10 -- Base-10 logarithm of a "bigint" object.
%%  log10(self) returns the base-10 logarithm of the absolute
%%   value of self, a non-zero "bigint" object.
%%
%%  Also see: log2, digits.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 16:49:24.
%
%if nargin < 1, help(mfilename), return, end
%
%result = digits(abs(self)) - 1;
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "log2.m" (text)')
fout = fopen('log2.m', 'w');
%function theResult = log2(self)
%
%% bigint/log2 -- Base-2 logarithm of a "bigint" object.
%%  log2(self) returns the base-2 logarithm of the absolute
%%   value of self, a "bigint" object.  The algorithm uses
%%   bisection.
%%
%%  Also see: root, mpower, log10.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 16:49:24.
%
%if nargin < 1, help(mfilename), return, end
%
%self = abs(bigint(self));
%
%result = bigint(1);
%
%two = bigint(2);
%
%while two^(2*result) <= self
%    result = 2*result;
%end
%
%delta = result;
%
%while delta > 1
%	delta = half(delta);
%	trial = result + delta;
%	if two^trial <= self
%		result = trial;
%	end
%end
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "lt.m" (text)')
fout = fopen('lt.m', 'w');
%function theResult = lt(self, other)
%
%% bigint/lt -- Less-than comparison of "bigint" objects.
%%  lt(self, other) returns true if self < other, for the
%%   two items, one of which must be a "bigint" object.
%%   This routine is the basis for all other logical
%%   comparisons.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = 0;
%
%self = bigint(self);
%other = bigint(other);
%
%if sign(self) < sign(other)
%    result = 1;
%else
%    a = magnitude(self);
%    b = magnitude(other);
%    while length(a) < length(b), a = [0 a]; end
%    while length(b) < length(a), b = [0 b]; end
%    f = find(a < b);
%    g = find(a > b);
%    if any(f) & ~any(g)
%        result = 1;
%    elseif any(f) & any(g) & f(1) < g(1)
%        result = 1;
%    else
%        result = 0;
%    end
%end
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "magnitude.m" (text)')
fout = fopen('magnitude.m', 'w');
%function theResult = magnitude(self, theMagnitude)
%
%% bigint/magnitude -- Magnitude of a "bigint" object.
%%  magnitude(self) returns the magnitude of self,
%%   expressed in base-1000 as a vector of "double".
%%  magnitude(self, theMagnitude) sets the magnitude
%%   of self to theMagnitude.
%%
%% Also see: sign.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:23:47.
%
%if nargin < 2
%    result = self.itsMagnitude;
%else
%    self.itsMagnitude = theMagnitude;
%    result = self;
%end
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "minus.m" (text)')
fout = fopen('minus.m', 'w');
%function theResult = minus(self, other)
%
%% bigint/minus -- Difference of two "bigint" objects.
%%  minus(self, other) returns (self - other), for
%%   the given objects, one of which must be a "bigint".
%%
%% Also see: plus.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%self = bigint(self);
%other = bigint(other);
%
%[self, other] = pad(self, other);
%
%a = magnitude(self) * sign(self);
%b = magnitude(other) * sign(other);
%
%if self < other
%    result = -bigint(b - a);
%else
%    result = bigint(a - b);
%end
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "mpower.m" (text)')
fout = fopen('mpower.m', 'w');
%function theResult = mpower(self, thePower)
%
%% bigint/mpower -- Raise a "bigint" to an integer power.
%%  mpower(self, thePower) returns self raised to thePower,
%%   for self, a "bigint", and thePower, a non-negative integer.
%%   This routine uses the successive-squaring algorithm.
%%
%% Also see: sqrt, root.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 15:24:29.
%
%if nargin < 2, help(mfilename), return, end
%
%self = bigint(self);
%
%thePower = abs(thePower);
%
%if thePower < 1
%	[num, den] = rat(double(thePower));
%	result = root(self^num(1), den(1));
%else
%	self = bigint(self);
%	thePower = bigint(thePower);
%	result = bigint(1);
%	factor = self;
%	while thePower > 0
%		remainder = rem(thePower, 2);
%		if remainder
%			result = result * factor;
%		end
%		thePower = half(thePower - remainder);
%		factor = factor * factor;
%	end
%end
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "mrdivide.m" (text)')
fout = fopen('mrdivide.m', 'w');
%function [theResult, theRemainder] = mrdivide(self, other)
%
%% bigint/mrdivide -- Quotient of two "bigint" objects.
%%  mrdivide(self, other) returns (self / other), for
%%   the given objects, one of which must be a "bigint".
%%   The algorithm uses bisection.
%%  [theResult, theRemainder] = mrdivide(self, other)
%%   also returns theRemainder when called explicitly.
%%   Otherwise, use "rem" for the remainder.
%%
%% Also see: mtimes, recip.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%self = bigint(self);
%other = bigint(other);
%
%theSign = sign(self)*sign(other);
%
%self = abs(self);
%other = abs(other);
%
%result = bigint(0);
%
%if self >= other
%	result = bigint(1);
%	while result*other*2 <= self
%		result = result*2;
%	end
%	delta = result;
%	while delta > 1
%		delta = half(delta);
%		trial = result + delta;
%		if trial*other <= self
%			result = trial;
%		end
%	end
%	result = sign(result, theSign);
%end
%
%
%if nargout > 0
%    theResult = result;
%	if nargout > 1, theRemainder = self - other*result; end
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "mtimes.m" (text)')
fout = fopen('mtimes.m', 'w');
%function theResult = mtimes(self, other)
%
%% bigint/mtimes -- Product of two "bigint" objects.
%%  mtimes(self, other) returns (self * other), for
%%   the given objects, one of which must be a "bigint".
%%
%% Also see: mrdivide.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%self = bigint(self);
%other = bigint(other);
%
%c = conv(magnitude(self), magnitude(other));
%
%result = sign(bigint(c), sign(self)*sign(other));
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "ne.m" (text)')
fout = fopen('ne.m', 'w');
%function theResult = ne(self, other)
%
%% bigint/ne -- Not-equal-to comparison for "bigint" objects.
%%  ne(self, other) returns true if self ~= other, for the
%%   two items, one of which must be a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 17:04:31.
%
%result = ~(self == other);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "not.m" (text)')
fout = fopen('not.m', 'w');
%function theResult = not(self)
%
%% bigint/not -- Logical-NOT of a "bigint" object.
%%  not(self, other) returns ~self, for self a "bigint".
%%
%% Also see: and, or.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 1, help(mfilename), return, end
%
%result = (abs(self) == 0);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "or.m" (text)')
fout = fopen('or.m', 'w');
%function result = or(self, other)
%
%% bigint/or -- Logical-OR of two "bigint" objects.
%%  or(self, other) returns (self & other), for
%%   the given objects, one of which must be a "bigint".
%%
%% Also see: and, not.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%result = (abs(self) ~= 0 | abs(other) ~= 0);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "pad.m" (text)')
fout = fopen('pad.m', 'w');
%function [theResult, theOther] = pad(self, other)
%
%% bigint/pad -- Pad two "bigint" objects to same length.
%%  [theResult, theOther] = pad(self, other) pads the
%%   magnitudes of self and other to the same length.
%%   At least one of the arguments must be a "bigint".
%%
%% Also see: trim.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 14:41:49.
%
%self = bigint(self);
%other = bigint(other);
%
%a = magnitude(self);
%b = magnitude(other);
%
%while length(a) < length(b), a = [0 a]; end
%while length(b) < length(a), b = [0 b]; end
%
%self = magnitude(self, a);
%other = magnitude(other, b);
%
%if nargout > 0
%    theResult = self;
%    theOther = other;
%else
%	result = {result, other};
%    assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "plus.m" (text)')
fout = fopen('plus.m', 'w');
%function theResult = plus(self, other)
%
%% bigint/plus -- Sum of two "bigint" objects.
%%  plus(self, other) returns (self + other), for
%%   the given objects, one of which must be a "bigint".
%%
%% Also see: minus.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:16:19.
%
%if nargin < 2, help(mfilename), return, end
%
%self = bigint(self);
%other = bigint(other);
%
%[self, other] = pad(self, other);
%
%a = magnitude(self) * sign(self);
%b = magnitude(other) * sign(other);
%
%result = bigint(a + b);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "recip.m" (text)')
fout = fopen('recip.m', 'w');
%function [theResult, theScaleFactor] = recip(self, nDigits)
%
%% bigint/recip -- Reciprocal of a bigint.
%%  [r, f] = recip(self) returns the reciprocal of self,
%%   multiplied by the factor of 10 just larger than self^2.
%%   The result is accurate to approximately the number
%%   of digits in self.  Example: the reciprocal of 31
%%   is r = 33; f = 1000.  The product 31*33 is 1023.
%%  [r, f] = recip(a, nDigits) returns a result that is
%%   accurate to the given number of decimal digits.
% 
%% Copyright (C) 2002 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 30-Jul-2002 11:40:37.
%% Updated    30-Jul-2002 14:12:05.
%
%if nargin < 1, help(mfilename), return, end
%
%extra_digits = 0;
%
%if nargin > 1
%	d = bigint(10)^nDigits;
%	while abs(self) < d
%		self = 10*self;
%		extra_digits = extra_digits + 1;
%	end
%end
%
%theScaleFactor = bigint(1);
%fmax = self*self;
%while theScaleFactor < fmax
%	theScaleFactor = 10*theScaleFactor;
%end
%
%result = self;
%prev = 0;
%
%% Newton iterations.
%
%for iter = 1:3*length(magnitude(theScaleFactor))
%	result = 2*result - (self*result*result)/theScaleFactor;   % 94 percent of run-time.
%	if result == prev, break, end
%	prev = result;
%end
%
%% Re-scale the multiplier to account
%%  for any extra digits.
%
%for i = 1:extra_digits
%	theScaleFactor = theScaleFactor / 10;
%end
%
%% Output.
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "rem.m" (text)')
fout = fopen('rem.m', 'w');
%function theResult = rem(self, theModulus)
%
%% bigint/rem -- Remainder for "bigint" object.
%%  rem(self, theModulus) returns the remainder from
%%   integer division of self by theModulus, one of
%%   which is a "bigint" object.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 17:26:42.
%
%if nargin < 2, help(mfilename), return, end
%
%quotient = self / theModulus;
%
%temp = self - quotient * theModulus;
%if temp < 0
%    temp = self + temp;
%    quitient = temp / theModulus;
%end
%
%result = self - quotient * theModulus;
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "root.m" (text)')
fout = fopen('root.m', 'w');
%function theResult = root(self, theRoot)
%
%% bigint/root -- Integer root of a "bigint" object.
%%  root(self, theRoot) returns the desired root of self,
%%   a "bigint" object, for theRoot, a positive-integer.
%%   The algorithm uses bisection.
%%
%% Also see: sqrt, mpower.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 15:24:29.
%
%if nargin < 2, help(mfilename), return, end
%
%self = abs(bigint(self));
%theRoot = abs(bigint(theRoot));
%
%result = bigint(1);
%while (result*2)^theRoot <= self
%	result = result*2;
%end
%
%delta = result;
%
%while delta > 1
%	delta = half(delta);
%	trial = result + delta;
%	if trial^theRoot <= self
%		result = trial;
%	end
%end
%
%if nargout > 0
%	theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "sign.m" (text)')
fout = fopen('sign.m', 'w');
%function theResult = sign(self, theSign)
%
%% bigint/sign -- Sign of a "bigint" object.
%%  sign(self) returns -1 or +1 for the sign of self,
%%   a "bigint" object.
%%  magnitude(self, theSign) sets the sign of self
%%   to theSign.
%%
%% Also see: magnitude.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:23:47.
%
%if nargin < 2
%    if self.itsSign < 0
%        result = -1;
%    else
%        result = +1;
%    end
%else
%    if theSign < 0
%        theSign = -1;
%    else
%        theSign = +1;
%    end
%    self.itsSign = theSign;
%    result = self;
%end
%
%if nargout > 0
%    theResult = result;
%else
%    assignin('caller', 'ans', result)
%	disp(result)
%end
fclose(fout);

disp(' ## Installing: "sqrt.m" (text)')
fout = fopen('sqrt.m', 'w');
%function theResult = sqrt(self)
%
%% bigint/sqrt -- Square-root of a "bigint" object.
%%  sqrt(self) returns the largest integer whose
%%   square does not exceed the absolute value of self,
%%   a "bigint" object.  The algorithm uses bisection.
%%
%%  Also see: root, mpower.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 16:49:24.
%
%if nargin < 1, help(mfilename), return, end
%
%self = abs(bigint(self));
%
%result = bigint(1);
%while result*result*4 <= self
%	result = result*2;
%end
%
%delta = result;
%
%while delta > 1
%	delta = half(delta);
%	trial = result + delta;
%	if trial^2 <= self
%		result = trial;
%	end
%end
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "trim.m" (text)')
fout = fopen('trim.m', 'w');
%function theResult = trim(self)
%
%% bigint/trim -- Trim leading zeros from a "bigint".
%%  trim(self) adjusts self, a "bigint" object, to
%%   mod(1000) form and removes its leading zeros.
%%
%% Also see: pad.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 21-Feb-1998 13:39:08.
%
%if nargin < 1, help(mfilename), return, end
%
%% Trim.
%
%a = magnitude(self);
%while length(a) > 1 & a(1) == 0, a(1) = []; end
%
%% Change sign.
%
%if a(1) < 0, self = -magnitude(self, -a); end
%
%a = magnitude(self);
%
%% Borrow.
%
%theBase = base(self);
%while any(a < 0)
%	while length(a) > 1 & a(1) == 0, a(1) = []; end
%	for i = 2:length(a)
%		a(i-1) = a(i-1) - 1;
%		a(i) = a(i) + theBase;
%	end
%end
%
%% Carry.
%
%carry = 0;
%for i = length(a):-1:1
%    a(i) = a(i) + carry;
%    remainder = rem(a(i), theBase);
%    carry = (a(i) - remainder) / theBase;
%    a(i) = remainder;
%end
%
%% Left-over carry.
%
%while carry > 0
%    a = [carry a];
%    remainder = rem(a(1), theBase);
%    carry = (a(1) - remainder) / theBase;
%    a(1) = remainder;
%end
%
%% Trim.
%
%while length(a) > 1 & a(1) == 0, a(1) = []; end
%
%result = magnitude(self, a);
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "uminus.m" (text)')
fout = fopen('uminus.m', 'w');
%function theResult = uminus(self)
%
%% bigint/uminus -- Unary-minus of a "bigint" object.
%%  uminus(self) changes the sign of self, a "bigint"
%%   object.
%%
%% Also see: uplus.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 14:33:07.
%
%result = sign(self, -sign(self));
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "uplus.m" (text)')
fout = fopen('uplus.m', 'w');
%function theResult = uplus(self)
%
%% bigint/uplus -- Unary-plus of a "bigint" object.
%%  uplus(self) returns self, a "bigint" object.
%%
%% Also see: uminus.
% 
%% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
%%  All Rights Reserved.
%%   Disclosure without explicit written consent from the
%%    copyright owner does not constitute publication.
% 
%% Version of 22-Feb-1998 14:33:07.
%
%result = self;
%
%if nargout > 0
%    theResult = result;
%else
%	assignin('caller', 'ans', result)
%    disp(result)
%end
fclose(fout);

disp(' ## Installing: "version.m" (text)')
fout = fopen('version.m', 'w');
%function version(self)
%
%% Version of 30-Jul-2002 16:16:36.
%
%helpdlg(help(mfilename), 'bigint')
fclose(fout);
cd ('..')
cd ('..')
disp(' ')
disp(' ## Place "bigint" in your Matlab path,')
disp(' ## then restart Matlab.  Execute "bigint demo"')
disp(' ## at the Matlab prompt.')
disp(' ')