Changeset 72b4d96 in tol


Ignore:
Timestamp:
Feb 8, 2012, 6:04:13 PM (15 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
b83b8ed3
Parents:
2c0158d
Message:

Fixes #1421
New built-in functions LeastCommonMultiple and GreatestCommonDivisor

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@4079 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tol/bmath/barith/arith.cpp

    r2c0158d r72b4d96  
    2727#include <tol/tol_bout.h>
    2828#include <tol/tol_barith.h>
     29#include <boost/math/common_factor_rt.hpp>
    2930
    3031BTraceInit("arith.cpp");
    … …  
    265266    while(N<n);
    266267}
     268
     269
     270//--------------------------------------------------------------------
     271int GreatestCommonDivisor(int a, int b)
     272
     273/*! Return the Greatest Common Divisor of two integers
     274 */
     275//--------------------------------------------------------------------
     276{
     277  return(boost::math::gcd(a,b));
     278}
     279
     280//--------------------------------------------------------------------
     281int LeastCommonMultiple(int a, int b)
     282
     283/*! Return the Least Common Multiple of two integers
     284 */
     285//--------------------------------------------------------------------
     286{
     287  return(boost::math::lcm(a,b));
     288}
     289
     290//--------------------------------------------------------------------
     291int GreatestCommonDivisor(const BArray<int>& a)
     292
     293/*! Return the Greatest Common Divisor of an array of integers
     294 */
     295//--------------------------------------------------------------------
     296{
     297  int s = a.Size();
     298  if(!s) { return(0); }
     299  int i;
     300  int gcd = a[0];
     301  for(i=1; i<s; i++)
     302  {
     303    gcd = boost::math::gcd(gcd,a[i]);
     304  }
     305  return(gcd);
     306}
     307
     308//--------------------------------------------------------------------
     309int LeastCommonMultiple(const BArray<int>& a)
     310
     311/*! Return the Least Common Multiple of an array of integers
     312 */
     313//--------------------------------------------------------------------
     314{
     315  int s = a.Size();
     316  if(!s) { return(0); }
     317  int i;
     318  int lcm = a[0];
     319  for(i=1; i<s; i++)
     320  {
     321    lcm = boost::math::lcm(lcm,a[i]);
     322  }
     323  return(lcm);
     324}
     325
     326
  • tol/bmath/barith/tol_barith.h

    r2c0158d r72b4d96  
    5050TOL_API void FirstPrimes(BInt n, BArray<BInt>& p);
    5151
     52TOL_API int GreatestCommonDivisor(int a, int b);
     53TOL_API int LeastCommonMultiple(int a, int b);
     54TOL_API int GreatestCommonDivisor(const BArray<int>& a);
     55TOL_API int LeastCommonMultiple(const BArray<int>& a);
     56
    5257inline ldouble Abs      (BInt a)  { return(fabsl(a)); }
    5358inline ldouble Sqrt     (BInt a)  { return(sqrtl(a)); }
  • tol/btol/real_type/datgra.cpp

    r2c0158d r72b4d96  
    3939#include <tol/tol_bmatfun.h>
    4040#include <tol/tol_bvmat.h>
     41#include <tol/tol_barith.h>
    4142#include <float.h>
    4243
    … …  
    10761077
    10771078
     1079//--------------------------------------------------------------------
     1080  DeclareContensClass(BDat, BDatTemporary, BDatGreatestCommonDivisor);
     1081  DefIntOpr(1, BDatGreatestCommonDivisor, "GreatestCommonDivisor", 1, 0,
     1082  "(Real x1, [ Real x2 , ...])",
     1083  I2("Returns the Greatest Common Divisor of a list of positive "
     1084     "integer numbers. If any given number is unklnown or is not "
     1085     "integer and positive the unknown value will be returned.",
     1086     "Devuelve el Máximo Común Divisor de una lista de números "
     1087     "enteros positivos. Si algún número es desconocido o no es entero "
     1088     "y positivo se devolverá el valor desconocido."),
     1089  BOperClassify::RealArythmetic_);
     1090  void BDatGreatestCommonDivisor::CalcContens()
     1091//--------------------------------------------------------------------
     1092{
     1093  int gcd = -1;
     1094  int n;
     1095  for(n=1; n<=NumArgs(); n++)
     1096  {
     1097    BDat& x = Dat(Arg(n));
     1098    if(x.IsUnknown() || (x<=0) || !x.IsInteger()) { return; }
     1099    if(gcd == -1) { gcd = (int)x.Value(); }
     1100    else          { gcd = GreatestCommonDivisor(gcd, (int)x.Value()); }
     1101  }
     1102  contens_ = gcd;
     1103}
     1104
     1105//--------------------------------------------------------------------
     1106  DeclareContensClass(BDat, BDatTemporary, BDatLeastCommonMultiple);
     1107  DefIntOpr(1, BDatLeastCommonMultiple, "LeastCommonMultiple", 1, 0,
     1108  "(Real x1, [ Real x2 , ...])",
     1109  I2("Returns the Greatest Common Divisor of a list of positive "
     1110     "integer numbers. If any given number is unklnown or is not "
     1111     "integer and positive the unknown value will be returned.",
     1112     "Devuelve el Máximo Común Divisor de una lista de números "
     1113     "enteros positivos. Si algún número es desconocido o no es entero "
     1114     "y positivo se devolverá el valor desconocido."),
     1115  BOperClassify::RealArythmetic_);
     1116  void BDatLeastCommonMultiple::CalcContens()
     1117//--------------------------------------------------------------------
     1118{
     1119  int lcm = -1;
     1120  int n;
     1121  for(n=1; n<=NumArgs(); n++)
     1122  {
     1123    BDat& x = Dat(Arg(n));
     1124    if(x.IsUnknown() || (x<=0) || !x.IsInteger()) { return; }
     1125    if(lcm == -1) { lcm = (int)x.Value(); }
     1126    else          { lcm = LeastCommonMultiple(lcm, (int)x.Value()); }
     1127  }
     1128  contens_ = lcm;
     1129}
     1130
Note: See TracChangeset for help on using the changeset viewer.