Skip to content

Commit

Permalink
Introduce kernel-level LCM_INT
Browse files Browse the repository at this point in the history
This is done using the gmp function mpz_lcm.
  • Loading branch information
markuspf committed Dec 12, 2017
1 parent 9ae06ac commit ba3cff6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
8 changes: 1 addition & 7 deletions lib/integer.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,7 @@ InstallGlobalFunction( IsPrimePowerInt,
##
#F LcmInt( <m>, <n> ) . . . . . . . . . . least common multiple of integers
##
InstallGlobalFunction(LcmInt,function ( n, m )
if m = 0 and n = 0 then
return 0;
else
return AbsInt( m / GcdInt( m, n ) * n );
fi;
end);
InstallGlobalFunction(LcmInt, LCM_INT);


#############################################################################
Expand Down
38 changes: 38 additions & 0 deletions src/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,43 @@ Obj FuncGCD_INT ( Obj self, Obj opL, Obj opR )
return GcdInt( opL, opR );
}

Obj LcmInt ( Obj opL, Obj opR )
{
UInt sizeL, sizeR;
fake_mpz_t mpzL, mpzR, mpzResult;
Obj result;

CHECK_INT(opL);
CHECK_INT(opR);

if (opL == INTOBJ_INT(0)) return AbsInt(opR);
if (opR == INTOBJ_INT(0)) return AbsInt(opL);

sizeL = SIZE_INT_OR_INTOBJ(opL);
sizeR = SIZE_INT_OR_INTOBJ(opR);

NEW_FAKEMPZ( mpzResult, sizeL + sizeR );
FAKEMPZ_GMPorINTOBJ( mpzL, opL );
FAKEMPZ_GMPorINTOBJ( mpzR, opR );

/* compute the gcd */
mpz_lcm( MPZ_FAKEMPZ(mpzResult), MPZ_FAKEMPZ(mpzL), MPZ_FAKEMPZ(mpzR) );

/* convert result to GAP object and return it */
CHECK_FAKEMPZ(mpzResult);
CHECK_FAKEMPZ(mpzL);
CHECK_FAKEMPZ(mpzR);
result = GMPorINTOBJ_FAKEMPZ( mpzResult );
CHECK_INT(result);
return result;
}

Obj FuncLCM_INT( Obj self, Obj opL, Obj opR )
{
REQUIRE_INT_ARG( "LcmInt", "left", opL );
REQUIRE_INT_ARG( "LcmInt", "right", opR );
return LcmInt( opL, opR );
}

/****************************************************************************
**
Expand Down Expand Up @@ -2621,6 +2658,7 @@ static StructGVarFunc GVarFuncs [] = {
GVAR_FUNC(SIGN_INT, 1, "x"),
GVAR_FUNC(REM_INT, 2, "gmp1, gmp2"),
GVAR_FUNC(GCD_INT, 2, "gmp1, gmp2"),
GVAR_FUNC(LCM_INT, 2, "gmp1, gmp2"),
GVAR_FUNC(PROD_INT_OBJ, 2, "gmp, obj"),
GVAR_FUNC(POW_OBJ_INT, 2, "obj, gmp"),
GVAR_FUNC(JACOBI_INT, 2, "n, m"),
Expand Down

0 comments on commit ba3cff6

Please sign in to comment.