Skip to content

Commit

Permalink
Minor update to NADICZ arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanuxem committed Feb 6, 2025
1 parent 5732abb commit 0103465
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 71 deletions.
12 changes: 6 additions & 6 deletions src/algebra/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ JULIALIST = EXPADICZ INECF JARBPR JCF32 JCF32VEC JCF32MAT JCF32SMA \
JCF64 JCF64MAT JCF64MTF JCF64SMA JCF64VEC JCFSF JCFLOAT JCF32LA \
JCF64LA JCRING JDFRAME JDRAW JF32 JF32AF JF32VEC JF32VEC2 JF32MAT \
JF32SMAT JF64 JF64AF JF64MAT JF64MTF JF32SF JF64SF JF32SF2 JF64SF2 \
JF64SMAT JF64VEC JF64VEC2 JFLOAT JFSF JFSF2 JI64 JI64VEC JMATCAT \
JMATRIX JMFLOAT JMTYPE JOBJECT JOBAGG JOBBOOL JOBBINT JOBCF32 JOBCF64 \
JOBDICT JOBDLINK JOBF32 JOBF64 JOBI64 JOBPAIR JOBRING JOBTPLE JOBTYPE \
JPLOT JRING JF32LA JF64LA JSTRU JSYM JUF JTYPE JVECCAT JVECTOR JVECTOR2 \
JF64SMAT JF64VEC JF64VEC2 JFLOAT JFSF JFSF2 JI64 JI64VEC JMATCAT \
JMATRIX JMFLOAT JMTYPE JOBJECT JOBAGG JOBBOOL JOBBINT JOBCF32 JOBCF64 \
JOBDICT JOBDLINK JOBF32 JOBF64 JOBI64 JOBPAIR JOBRING JOBTPLE JOBTYPE \
JPLOT JRING JF32LA JF64LA JSTRU JSYM JUF JTYPE JVECCAT JVECTOR JVECTOR2 \
NACF NACFS NAN NACB NARB NCB NCF NCRING NECF NFF NFR NFRAC NPADICC \
NINT NMLP NMP NPADICZ NPF NRB NRF NRING NTYPE NULP NUP NZMOD
NINT NMLP NMP NPADIC2 NPADICZ NPF NRB NRF NRING NTYPE NULP NUP NZMOD
JULIACATLIST = JARBPR JCRING JMATCAT JMFLOAT JMTYPE JOBAGG JOBRING JOBTYPE \
JRING JTYPE JVECCAT NACF NACFS NCRING NPADICC NRING NTYPE
JULIACATDOMS = JMTYPE JOBAGG JOBRING JOBTYPE
JULIACATDOMS = JMTYPE JOBAGG JOBRING JOBTYPE NRING
ifeq ($(JULIA_USE_MLINK),yes)
JULIALIST += JWSAGG JWSAPCPX JWSAPR JWSCPLX JWSEXPR JWSGINT JWSINT JWSLIST JWSMAT \
JWSNUM JWSNSF JWSO JWSRAT JWSREAL JWSRING JWSSYM JWSUF JWSVEC
Expand Down
1 change: 1 addition & 0 deletions src/algebra/exposed.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@
(|NewSparseMultivariatePolynomial| . NSMP)
(|NewSparseUnivariatePolynomial| . NSUP)
(|NewSparseUnivariatePolynomialFunctions2| . NSUP2)
(|NMPadic2| . NPADIC2)
(|None| . NONE)
(|NonLinearFirstOrderODESolver| . NODE1)
(|NonLinearSolvePackage| . NLINSOL)
Expand Down
57 changes: 45 additions & 12 deletions src/algebra/jnemo.spad
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,54 @@ NMType() : Category == JLObjectType
++ Description:
++ Parent category of NM ring domains.
NMRing() : Category == Join(NMType, JLObjectRing) with
jlNMRing : () -> String
++ jlNMRing(x) returns the NM ring type of x as a string.
jlObject: () -> String
++ jlObject() returns the internal JL name of the module used.
exact? : % -> Boolean
++ exact?(x) heck wheter or not x is exact.
unit? : % -> Boolean
++ unit?(x) checks whther or not x is a unit, i.e. invertible.
equal? : (% , %) -> Boolean
++
characteristic : % -> NonNegativeInteger
++ characteristic**
inverse : % -> %
++ inverse
exactDivide : (%, %) -> %
++
divide : (%, %) -> Record(quotient : %, remainder : %)
++
jlNMRing : () -> String
++ jlNMRing(x) returns the NM ring type of x as a string.
jlObject: () -> String
++ jlObject() returns the internal JL name of the module used.
add
import from JLUtilityFunctions
import from String
jl_eval_string("import AbstractAlgebra: parent_type, elem_type, _
base_ring, base_ring_type, parent, is_domain_type, _
is_exact_type, canonical_unit, isequal, divexact, _
get_cached!, is_unit, expressify, _
@show_name, @show_special, is_terse, pretty, terse, Lowercase")$Lisp

getind(a) ==> concat(["getindex(refs,", string(jlId(a)), ")"])
junfunc(func, a) ==> make_jlref_wcall1(func, a)$Lisp
jbinfunc(func,a,b) ==> make_jlref_wcall2(func, a, b)$Lisp
junbfunc(bfunc, a) ==> jl_call1_bool_wrapped_index(bfunc, jlId(a))$Lisp
jbinbfunc(op,a,b) ==> jl_call2_bool_wrapped_index(op, jlId(a), jlId(b))$Lisp
exact?(x) == junbfunc("is_exact_type",x)
unit?(x) == junbfunc("is_unit",x)
exactDivide(x,y) == jbinfunc("divexact",x,y)
divide(a,b) : Record(quotient : %, remainder : %) ==
ret : JLObjTuple := jbinfunc("divrem",a,b)
[ret.1 pretend %, ret.2 pretend %]
equal?(x,y) == jbinbfunc("isequal",x,y)
characteristic(x) == junfunc("characteristic",junfunc("base_ring",x))
inverse(x) == junfunc("inv",x)

)abbrev category NCRING NMCommutativeRing
++ Category for NM commutative ring domains.
++ Author: G.Vanuxem
++ Date Created: Dec,, 2024
++ Description:
++ Parent category of NM coommutative ring domains.
++ Parent category of NM commutative ring domains.
NMCommutativeRing() : Category == Join(NMRing, CommutativeRing)

)abbrev category JARBPR JLArbitraryPrecision
Expand Down Expand Up @@ -70,6 +107,8 @@ NMInteger() : Exports == Implementation where
++ coerce \undocumented
coerce : % -> AlgebraicNumber
++ coerce \undocumented
inverse : % -> NMFraction(NMInteger)
++
jnint : Integer -> %
++ jnint \undocumented
Implementation ==> add
Expand Down Expand Up @@ -138,16 +177,14 @@ NMInteger() : Exports == Implementation where
-- x quo y == jbinfuncdiv", x, y)
x quo y == jbinfunc("÷", x, y)
x rem y == jbinfunc("%", x, y)
divide(a,b) ==
ret : JLObjTuple := jbinfunc("divrem",a,b)
[ret.1 pretend %, ret.2 pretend %]
gcd(a,b) == jbinfunc("gcd",a,b)
lcm(a,b) == jbinfunc("lcm",a,b)
(p1:% exquo p2:%): Union(%,"failed") ==
ret : JLObjTuple := jbinfunc("divides", p1, p2)
not jlEvalString(concat(["first(", getind(ret), ")"]))@Boolean
=> "failed"
junfunc("last", ret)
inverse(x) == junfunc("inv",x)

coerce(jsi : %) : JI64 ==
ret : Integer := parsei string jsi
Expand Down Expand Up @@ -1681,10 +1718,6 @@ NMFraction(R : Join(NMCommutativeRing, IntegralDomain)) :
den : Integer := convert(junfunc("denominator", x) pretend NMInteger)
num/den
gcd(x,y) == jbinfunc("gcd",x,y)
divide(a,b) ==
ret : JLObjTuple := jbinfunc("divrem",a,b)
[ret.1 pretend %, ret.2 pretend %]

random(seg : Segment(Integer)) ==
jlref(concat(["rand(",NFR,",", string(low(seg)),":",
string(high(seg)),")"]))
Expand Down
98 changes: 60 additions & 38 deletions src/algebra/jnpadic.spad
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,31 @@ NMPadicIntegerCategory(p) : Category == Definition where
++ References:
++ Description:
++ This domain implements Zp, the p-adic completion of the integers.
ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementation where
ExtendedNMPadicInteger(p : NMInteger, prec : NMInteger) : Exports == Implementation where
--unBalanced? : Boolean
I ==> Integer
NNI ==> NonNegativeInteger
OUT ==> OutputForm
L ==> List
NINT ==> NMInteger
NFINT ==> NMFraction(NMInteger)
JI64 ==> JLInt64
Exports ==> Join(NMCommutativeRing, NMPadicIntegerCategory(p)) with
JI64 ==> NMInteger
Exports ==> Join(NMRing, NMPadicIntegerCategory(p)) with
jlApprox? : (%, %) -> Boolean
"+" : (%, %) -> %
++
"-" : (%, %) -> %
++
"/" : (%, %) -> %
++
"*" : (%, %) -> %
++
"+" : (NINT, %) -> %
++ a + x is the addition of a Julia Nemo Integer
++ and a p-adic Integer.
"+" : (%, I) -> %
++ x + a is the addition of a p-adic integer
++ and an Integer.
"+" : (%, NINT) -> %
++ x is the addition of a p-adic integer
++ and a Julia Nemo integer.
"+" : (I, %) -> %
++ a + x is the addition of an Integer and a p-adic integer.
++ For example:
++ \example{p := 1 + 2*7 + 4*7^2 + O(7,3)$NPADICZ(7)}
"+" : (%, NFINT) -> %
++ x is the addition of a p-adic integer
++ and a Julia Nemo Fraction integer.
Expand All @@ -92,14 +94,10 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio
++ and a p-adic integer
"*" : (NINT, %) -> %
++ a * x is the multiplication of a NemoInteger and a p-adic integer.
"*" : (I, %) -> %
++ a * x is the multiplication of an Integer and a p-adic integer.
"*" : (%, NINT) -> %
++ a * x is the multiplication of an Integer and a p-adic integer.
++ For example:
++ \example{p := 1 + 2*7 + 4*7^2 + O(7,3)$NPADICZ(7)}
"*" : (%, I) -> %
++ a * x is the multiplication of a Nemo integer and an integer.
sqrt : % -> %
++ sqrt(x) returns square root of x if
valuation : % -> %
Expand All @@ -118,18 +116,20 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio
coerce : NMInteger -> %
++ coerce(x) returns x as the p-adic completion of the NM Integer.
coerce: NMFraction(NMInteger) -> %
++
jpadic : NMInteger -> %
++
jnpadic : Integer -> %
++ padic(x) returns x as the p-adic completion of the NM Integer.
jnpadic : NMInteger -> %
++ padic(x) returns x as the p-adic completion of the NM Integer.
jpadic : NMFraction NMInteger -> %
jnpadic : NMFraction NMInteger -> %
++ padic(x) returns x as the p-adic completion of the NM Integer.
O : (NMInteger, JLInt64) -> %
O : (NMInteger, NMInteger) -> %
++ 0() creates the default Big-oh from domain parameters.
O : () -> %
++ 0() returns the default Big-oh from domain parameters.
prime : () -> NMInteger
++
precision : % -> Integer
precision : % -> NMInteger
++
Implementation ==> add
import from JLUtilityFunctions
Expand All @@ -139,9 +139,11 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio
jlImport "AbstractAlgebra.O"

getind(a) ==> concat(["getindex(refs,", string(jlId(a)), ")"])
jbinop(op,a,b) ==> jlref(concat([getind(a), op, getind(b)]))
junfunc(func, a) ==> make_jlref_wcall1(func, a)$Lisp
jbinfunc(func,a,b) ==> make_jlref_wcall2(func, a, b)$Lisp
junbfunc(bfunc, a) ==> jl_call1_bool_wrapped_index(bfunc, jlId(a))$Lisp
jbinbop(op,a,b) ==> jlEvalString(concat([getind(a), op, getind(b)]))@Boolean
jbinbfunc(op,a,b) ==>
jl_call2_bool_wrapped_index(op, jlId(a), jlId(b))$Lisp

Expand All @@ -164,8 +166,8 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio

--euclideanSize(x) == order(retract x)

prime() == jlref(concat(["prime(",jlNMRing(),")"]))$NMInteger
precision(x) == jlEvalString(concat ["precision(", getind(x),")"])
prime() == p
precision(x) == prec
sqrt(x) == junfunc("sqrt",x)
valuation(x) == junfunc("valuation",x)
exp(x) == junfunc("exp",x)
Expand Down Expand Up @@ -193,7 +195,7 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio
--invModP : I -> I
--invModP n == invmod(n, p)

modulus() == jlref(concat ["prime(",jlNMRing(), ")"])$NMInteger
modulus() == p
--moduloP x == (empty? x => 0; frst x)
--quotientByP x == (empty? x => x; rst x)

Expand All @@ -220,23 +222,28 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio

0 == jlref(concat ["zero(", EXPADICZP, ")"])
1 == jlref(concat ["one(", EXPADICZP, ")"])
a : NINT + pa : % == jbinfunc("+",a,pa)
a : Integer + pa : % == jbinfunc("+",jnint(a),pa)
a : NINT * pa : % == jbinfunc("*",a,pa)
a : Integer * pa : % == jbinfunc("*",jnint(a),pa)
pa : % + a : NINT == jbinfunc("+",pa,a)
pa : % + a : I == jbinfunc("+", pa, jnint a)
pa : % * a : NINT == jbinfunc("*", pa, a)
pa : % * a : I == jbinfunc("*", pa, jnint a)
pa : % * a : NFINT == jbinfunc("*", pa, a)
a : NFINT * pa: % == jbinfunc("*", a, pa)
pa : % + a : NFINT == jbinfunc("+", pa, a)
a : NFINT + pa: % == jbinfunc("+", a, pa)
coerce(i : Integer) == junfunc(EXPADICZPP, string(i))
pa : % = pb : % == jbinbop("==", pa,pb)
pa : % ~= pb : % == not jbinbop("==", pa,pb)
pa : % + pb : % == jlref(concat([getind(pa),"+",getind(pb)]))
pa : % - pb : % == jlref(concat([getind(pa),"-",getind(pb)]))
pa : % / pb : % == jlref(concat([getind(pa),"//",getind(pb)]))
pa : % * pb : % == jlref(concat([getind(pa),"*",getind(pb)]))


a : NINT + pa : % == jbinop("+",a,pa)
a : NINT * pa : % == jbinop("*",a,pa)
pa : % + a : NINT == jbinop("+",pa,a)
pa : % * a : NINT == jbinop("*", pa, a)
pa : % * a : NFINT == jbinop("*", pa, a)
a : NFINT * pa: % == jbinop("*", a, pa)
pa : % + a : NFINT == jbinop("+", pa, a)
a : NFINT + pa: % == jbinop("+", a, pa)
coerce(i : Integer) == jlref(concat([EXPADICZPP, getind(jnint(i)),")"]))
coerce(i : NMInteger) == junfunc(EXPADICZP, i)
coerce(fi : NMFraction(NMInteger)) == junfunc(EXPADICZP, fi)
jpadic(x) == jlref(concat([EXPADICZPP, string(x),")"]))
jpadic(fi : NMFraction(NMInteger)) == junfunc(EXPADICZP, fi)
jnpadic(x:Integer) == jlref(concat([EXPADICZPP, getind(jnint(x)),")"]))
jnpadic(fi : NMFraction(NMInteger)) == junfunc(EXPADICZP, fi)
jnpadic(fi : NMFraction(NMInteger)) == junfunc(EXPADICZP, fi)
coerce(x) : OUT == string(x) pretend OUT

)abbrev domain NPADICZ NMPadicInteger
Expand All @@ -252,4 +259,19 @@ ExtendedNMPadicInteger(p : NMInteger, prec : JLInt64) : Exports == Implementatio
++ Description:
++ Implementation of Zp: p-adic numbers are represented as
++ sum(i = 0.., a[i] * p^i), where the a[i] lie in 0, 1, ..., (p - 1).
NMPadicInteger(p : NMInteger) == ExtendedNMPadicInteger(p, 64::JLInt64)
NMPadicInteger(p : NMInteger) == ExtendedNMPadicInteger(p, 64::NMInteger)

)abbrev domain NPADIC2 NMPadic2
++ Author: Grégory Vanuxem
++ Date Created: 3 February 2025
++ Basic Operations:
++ Related Domains: ExtendedNMPadicInteger
++ Also See: NMPadicIntegerCategory
++ AMS Classifications:
++ Keywords: p-adic, completion
++ Examples:
++ References:
++ Description:
++ Implementation of Zp: p-adic numbers are represented as
++ sum(i = 0.., a[i] * p^i), where the a[i] lie in 0, 1, ..., (p - 1).
NMPadic2() == ExtendedNMPadicInteger(2::NMInteger, 30::NMInteger)
11 changes: 0 additions & 11 deletions src/algebra/jnpoly.spad
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ NMUnivariatePolynomial(R : NMRing, x : Symbol):
not jlEvalString(concat(["first(", getind(ret), ")"]))@Boolean
=> "failed"
junfunc("last", ret)
divide(a,b) : Record(quotient : %, remainder : %) ==
ret : JLObjTuple := jbinfunc("divrem",a,b)
[ret.1 pretend %, ret.2 pretend %]
a:% quo b:% == jbinfunc("div", a, b)
a:% rem b:% == jbinfunc("mod", a, b)
pseudoRemainder(p1,p2) == jbinfunc("pseudorem",p1,p2)
Expand Down Expand Up @@ -361,14 +358,6 @@ NMUnivariateLaurentPolynomial(R : NMRing, x : Symbol):
++ p * n is the product of p with a non negative integer.
"*" : (%, PI) -> %
++ p * n is the product of p with a positive integer.
exactDivide : (%, %) -> %
++ exactDivide(p,q) divides p by q if the division is exact.
--pseudoDivide : (%, %) -> Record(quotient : %, remainder : %)
--++ pseudoDivide(p1,p2) returns pseudo-quotient and
--++ pseudo-remainder of the pseudo-division of p1 by p2.
divide : (%, %) -> Record(quotient: %, remainder : %)
++ divide(p,q) returns a record (quotient, remainder) of the
++ the Euclidean division of p by q.
_quo : (%, %) -> %
++ a quo b returns the quotient of \spad{a} and b, forgetting
++ the remainder.
Expand Down
14 changes: 10 additions & 4 deletions src/algebra/jobject.spad
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ JLObjectType() : Category == JLType() with
jlRef : % -> SExpression
++ jlRef(obj) returns the pretty printed internal Lisp representation
++ of the JL object obj.
jlDump : JLObject -> Void
++ jlDump(obj) dumps (shows) the Julia object (internal structure with type).
mutable? : % -> Boolean
++ mutable?(obj) checks whether or not obj is mutable.
nothing? : % -> Boolean
Expand Down Expand Up @@ -80,6 +82,7 @@ JLObjectType() : Category == JLType() with
jlId(obj) == JLREFID(obj)$Lisp
jlType(obj) == jlEvalString(concat(["string(typeof(",getind(obj),"))"]))
jlRef(obj) == obj pretend SExpression
jlDump(obj) == jlEvalString(concat(["dump(", getind(obj),")"]))

x = y == jl_call2_bool_wrapped_index("==", jlId(x), jlId(y))$Lisp
x ~= y == jl_call2_bool_wrapped_index("!=", jlId(x), jlId(y))$Lisp
Expand Down Expand Up @@ -1433,12 +1436,15 @@ JLDataFrame() : Exports == Implementation where
import from JLUtilityFunctions
import from String

not jlUsing "DataFrames" =>
error "JLDataFrame: DataFrames is not installed in JL"
not jlUsing "DataFrames,Statistics" =>
error "JLDataFrame: DataFrames is not installed in Julia"
-- jlEvalString("macro dfa(obj) DataFrame(eval(Meta.parse(obj)),:auto) end")@Void

Rep := SExpression
jlEvalString("function dfa(obj) DataFrame(obj, :auto) end")@Void

Rep := SExpression


junfunc(func, a) ==> make_jlref_wcall1(func, a)$Lisp

elt(obj : %, jsy : JLSymbol) ==
Expand Down Expand Up @@ -1642,7 +1648,7 @@ JLMatrix(R : JLObjectRing) : Exports == Implementation where
Row ==> JLVector(R)
Col ==> JLVector(R)
MLAF ==> MatrixLinearAlgebraFunctions(R, Row, Col, %)
parsei ==> PARSE_-INTEGER$Lisp
parsei ==> PARSE_-INTEGER$Lisp
Exports ==> Join(JLObjectAggregate,
JLMatrixCategory(R, Row, Col)) with
new : (NNI, NNI, R) -> %
Expand Down
1 change: 1 addition & 0 deletions src/interp/macros.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
((IDENTP V) NIL)
((STRINGP U) (AND (STRINGP V) (string> V U) T))
((STRINGP V) NIL)
((or (TYPEP U 'JLREF) (TYPEP V 'JLREF)) NIL)
((BREAK))
((croak "Do not understand")))
T))
Expand Down
1 change: 1 addition & 0 deletions src/lib/julia_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,7 @@ void jl_init_env(void){
jl_eval_str("using Random, LinearAlgebra, SpecialFunctions, Suppressor");
jl_eval_str("import REPL; import InteractiveUtils.run");
jl_eval_str("cgM0yG8bklq9FmsK=[1.0 2;3 3];evET3JUgH1R2h9yC=similar(cgM0yG8bklq9FmsK);mul!(evET3JUgH1R2h9yC,cgM0yG8bklq9FmsK,cgM0yG8bklq9FmsK);gamma(1);polygamma(1,1);digamma(1);erf(0);erfi(0);erfc(0);zeta(0)");
//jl_eval_str("@suppress_err using AbstractAlgebra");
jl_eval_str("@suppress_err using Nemo");
}

Expand Down

0 comments on commit 0103465

Please sign in to comment.