Z3
 
Loading...
Searching...
No Matches
RCFNum.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2024 Microsoft Corporation
3
4Module Name:
5
6 RCFNum.cs
7
8Abstract:
9
10 Z3 Managed API: Real Closed Field (RCF) Numerals
11
12Author:
13
14 GitHub Copilot 2024-01-12
15
16Notes:
17
18--*/
19using System.Diagnostics;
20using System;
21
22namespace Microsoft.Z3
23{
33 public class RCFNum : Z3Object
34 {
40 public RCFNum(Context ctx, string value)
41 : base(ctx, Native.Z3_rcf_mk_rational(ctx.nCtx, value))
42 {
43 Debug.Assert(ctx != null);
44 }
45
51 public RCFNum(Context ctx, int value)
52 : base(ctx, Native.Z3_rcf_mk_small_int(ctx.nCtx, value))
53 {
54 Debug.Assert(ctx != null);
55 }
56
60 internal RCFNum(Context ctx, IntPtr obj)
61 : base(ctx, obj)
62 {
63 Debug.Assert(ctx != null);
64 }
65
71 public static RCFNum MkPi(Context ctx)
72 {
73 return new RCFNum(ctx, Native.Z3_rcf_mk_pi(ctx.nCtx));
74 }
75
81 public static RCFNum MkE(Context ctx)
82 {
83 return new RCFNum(ctx, Native.Z3_rcf_mk_e(ctx.nCtx));
84 }
85
91 public static RCFNum MkInfinitesimal(Context ctx)
92 {
93 return new RCFNum(ctx, Native.Z3_rcf_mk_infinitesimal(ctx.nCtx));
94 }
95
104 public static RCFNum[] MkRoots(Context ctx, RCFNum[] coefficients)
105 {
106 if (coefficients == null || coefficients.Length == 0)
107 {
108 throw new Z3Exception("Polynomial coefficients cannot be empty");
109 }
110
111 uint n = (uint)coefficients.Length;
112 IntPtr[] a = new IntPtr[n];
113 IntPtr[] roots = new IntPtr[n];
114
115 for (uint i = 0; i < n; i++)
116 {
117 a[i] = coefficients[i].NativeObject;
118 }
119
120 uint numRoots = Native.Z3_rcf_mk_roots(ctx.nCtx, n, a, roots);
121
122 RCFNum[] result = new RCFNum[numRoots];
123 for (uint i = 0; i < numRoots; i++)
124 {
125 result[i] = new RCFNum(ctx, roots[i]);
126 }
127
128 return result;
129 }
130
136 public RCFNum Add(RCFNum other)
137 {
138 CheckContext(other);
139 return new RCFNum(Context, Native.Z3_rcf_add(Context.nCtx, NativeObject, other.NativeObject));
140 }
141
147 public RCFNum Sub(RCFNum other)
148 {
149 CheckContext(other);
150 return new RCFNum(Context, Native.Z3_rcf_sub(Context.nCtx, NativeObject, other.NativeObject));
151 }
152
158 public RCFNum Mul(RCFNum other)
159 {
160 CheckContext(other);
161 return new RCFNum(Context, Native.Z3_rcf_mul(Context.nCtx, NativeObject, other.NativeObject));
162 }
163
169 public RCFNum Div(RCFNum other)
170 {
171 CheckContext(other);
172 return new RCFNum(Context, Native.Z3_rcf_div(Context.nCtx, NativeObject, other.NativeObject));
173 }
174
179 public RCFNum Neg()
180 {
181 return new RCFNum(Context, Native.Z3_rcf_neg(Context.nCtx, NativeObject));
182 }
183
188 public RCFNum Inv()
189 {
190 return new RCFNum(Context, Native.Z3_rcf_inv(Context.nCtx, NativeObject));
191 }
192
198 public RCFNum Power(uint k)
199 {
200 return new RCFNum(Context, Native.Z3_rcf_power(Context.nCtx, NativeObject, k));
201 }
202
206 public static RCFNum operator +(RCFNum a, RCFNum b)
207 {
208 return a.Add(b);
209 }
210
214 public static RCFNum operator -(RCFNum a, RCFNum b)
215 {
216 return a.Sub(b);
217 }
218
222 public static RCFNum operator *(RCFNum a, RCFNum b)
223 {
224 return a.Mul(b);
225 }
226
230 public static RCFNum operator /(RCFNum a, RCFNum b)
231 {
232 return a.Div(b);
233 }
234
238 public static RCFNum operator -(RCFNum a)
239 {
240 return a.Neg();
241 }
242
248 public bool Lt(RCFNum other)
249 {
250 CheckContext(other);
251 return 0 != Native.Z3_rcf_lt(Context.nCtx, NativeObject, other.NativeObject);
252 }
253
259 public bool Gt(RCFNum other)
260 {
261 CheckContext(other);
262 return 0 != Native.Z3_rcf_gt(Context.nCtx, NativeObject, other.NativeObject);
263 }
264
270 public bool Le(RCFNum other)
271 {
272 CheckContext(other);
273 return 0 != Native.Z3_rcf_le(Context.nCtx, NativeObject, other.NativeObject);
274 }
275
281 public bool Ge(RCFNum other)
282 {
283 CheckContext(other);
284 return 0 != Native.Z3_rcf_ge(Context.nCtx, NativeObject, other.NativeObject);
285 }
286
292 public bool Eq(RCFNum other)
293 {
294 CheckContext(other);
295 return 0 != Native.Z3_rcf_eq(Context.nCtx, NativeObject, other.NativeObject);
296 }
297
303 public bool Neq(RCFNum other)
304 {
305 CheckContext(other);
306 return 0 != Native.Z3_rcf_neq(Context.nCtx, NativeObject, other.NativeObject);
307 }
308
312 public static bool operator <(RCFNum a, RCFNum b)
313 {
314 return a.Lt(b);
315 }
316
320 public static bool operator >(RCFNum a, RCFNum b)
321 {
322 return a.Gt(b);
323 }
324
328 public static bool operator <=(RCFNum a, RCFNum b)
329 {
330 return a.Le(b);
331 }
332
336 public static bool operator >=(RCFNum a, RCFNum b)
337 {
338 return a.Ge(b);
339 }
340
344 public static bool operator ==(RCFNum a, RCFNum b)
345 {
346 if (ReferenceEquals(a, b)) return true;
347 if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false;
348 return a.Eq(b);
349 }
350
354 public static bool operator !=(RCFNum a, RCFNum b)
355 {
356 return !(a == b);
357 }
358
363 public bool IsRational()
364 {
365 return 0 != Native.Z3_rcf_is_rational(Context.nCtx, NativeObject);
366 }
367
372 public bool IsAlgebraic()
373 {
374 return 0 != Native.Z3_rcf_is_algebraic(Context.nCtx, NativeObject);
375 }
376
381 public bool IsInfinitesimal()
382 {
383 return 0 != Native.Z3_rcf_is_infinitesimal(Context.nCtx, NativeObject);
384 }
385
390 public bool IsTranscendental()
391 {
392 return 0 != Native.Z3_rcf_is_transcendental(Context.nCtx, NativeObject);
393 }
394
400 public string ToString(bool compact)
401 {
402 return Native.Z3_rcf_num_to_string(Context.nCtx, NativeObject, compact ? (byte)1 : (byte)0, 0);
403 }
404
409 public override string ToString()
410 {
411 return ToString(false);
412 }
413
419 public string ToDecimal(uint precision)
420 {
421 return Native.Z3_rcf_num_to_decimal_string(Context.nCtx, NativeObject, precision);
422 }
423
427 public override bool Equals(object obj)
428 {
429 if (obj is RCFNum other)
430 {
431 return this == other;
432 }
433 return false;
434 }
435
439 public override int GetHashCode()
440 {
441 return NativeObject.GetHashCode();
442 }
443
444 #region Internal
445 internal override void DecRef(IntPtr o)
446 {
447 Native.Z3_rcf_del(Context.nCtx, o);
448 }
449
450 private void CheckContext(RCFNum other)
451 {
452 if (Context != other.Context)
453 {
454 throw new Z3Exception("RCF numerals from different contexts");
455 }
456 }
457 #endregion
458 }
459}
The main interaction with Z3 happens via the Context.
Definition Context.cs:34
Context()
Constructor.
Definition Context.cs:39
Real Closed Field (RCF) numerals.
Definition RCFNum.cs:34
bool IsTranscendental()
Check if this RCF numeral is a transcendental number.
Definition RCFNum.cs:390
RCFNum Power(uint k)
Raise this RCF numeral to a power.
Definition RCFNum.cs:198
bool Le(RCFNum other)
Check if this RCF numeral is less than or equal to another.
Definition RCFNum.cs:270
RCFNum Div(RCFNum other)
Divide two RCF numerals.
Definition RCFNum.cs:169
RCFNum Sub(RCFNum other)
Subtract two RCF numerals.
Definition RCFNum.cs:147
bool Gt(RCFNum other)
Check if this RCF numeral is greater than another.
Definition RCFNum.cs:259
RCFNum Mul(RCFNum other)
Multiply two RCF numerals.
Definition RCFNum.cs:158
bool Ge(RCFNum other)
Check if this RCF numeral is greater than or equal to another.
Definition RCFNum.cs:281
static RCFNum[] MkRoots(Context ctx, RCFNum[] coefficients)
Find roots of a polynomial.
Definition RCFNum.cs:104
bool IsInfinitesimal()
Check if this RCF numeral is an infinitesimal.
Definition RCFNum.cs:381
static bool operator!=(RCFNum a, RCFNum b)
Operator overload for inequality.
Definition RCFNum.cs:354
RCFNum Inv()
Compute the multiplicative inverse.
Definition RCFNum.cs:188
static RCFNum operator*(RCFNum a, RCFNum b)
Operator overload for multiplication.
Definition RCFNum.cs:222
string ToDecimal(uint precision)
Convert this RCF numeral to a decimal string.
Definition RCFNum.cs:419
bool IsAlgebraic()
Check if this RCF numeral is an algebraic number.
Definition RCFNum.cs:372
static bool operator<=(RCFNum a, RCFNum b)
Operator overload for less than or equal.
Definition RCFNum.cs:328
override int GetHashCode()
Override GetHashCode for proper equality semantics.
Definition RCFNum.cs:439
static bool operator<(RCFNum a, RCFNum b)
Operator overload for less than.
Definition RCFNum.cs:312
RCFNum Add(RCFNum other)
Add two RCF numerals.
Definition RCFNum.cs:136
string ToString(bool compact)
Convert this RCF numeral to a string.
Definition RCFNum.cs:400
static RCFNum operator/(RCFNum a, RCFNum b)
Operator overload for division.
Definition RCFNum.cs:230
bool Neq(RCFNum other)
Check if this RCF numeral is not equal to another.
Definition RCFNum.cs:303
static RCFNum MkPi(Context ctx)
Create an RCF numeral representing pi.
Definition RCFNum.cs:71
RCFNum Neg()
Negate this RCF numeral.
Definition RCFNum.cs:179
static RCFNum operator+(RCFNum a, RCFNum b)
Operator overload for addition.
Definition RCFNum.cs:206
override string ToString()
Convert this RCF numeral to a string (non-compact).
Definition RCFNum.cs:409
override bool Equals(object obj)
Override Equals for proper equality semantics.
Definition RCFNum.cs:427
bool Lt(RCFNum other)
Check if this RCF numeral is less than another.
Definition RCFNum.cs:248
static RCFNum MkInfinitesimal(Context ctx)
Create an RCF numeral representing an infinitesimal.
Definition RCFNum.cs:91
static bool operator>=(RCFNum a, RCFNum b)
Operator overload for greater than or equal.
Definition RCFNum.cs:336
RCFNum(Context ctx, string value)
Create an RCF numeral from a rational string.
Definition RCFNum.cs:40
bool IsRational()
Check if this RCF numeral is a rational number.
Definition RCFNum.cs:363
static bool operator>(RCFNum a, RCFNum b)
Operator overload for greater than.
Definition RCFNum.cs:320
static RCFNum MkE(Context ctx)
Create an RCF numeral representing e (Euler's constant).
Definition RCFNum.cs:81
bool Eq(RCFNum other)
Check if this RCF numeral is equal to another.
Definition RCFNum.cs:292
RCFNum(Context ctx, int value)
Create an RCF numeral from a small integer.
Definition RCFNum.cs:51
static bool operator==(RCFNum a, RCFNum b)
Operator overload for equality.
Definition RCFNum.cs:344
static RCFNum operator-(RCFNum a, RCFNum b)
Operator overload for subtraction.
Definition RCFNum.cs:214
The exception base class for error reporting from Z3.
Internal base class for interfacing with native Z3 objects. Should not be used externally.
Definition Z3Object.cs:33
Context Context
Access Context object.
Definition Z3Object.cs:111
Z3_rcf_num Z3_API Z3_rcf_mk_rational(Z3_context c, Z3_string val)
Return a RCF rational using the given string.
Z3_rcf_num Z3_API Z3_rcf_mk_small_int(Z3_context c, int val)
Return a RCF small integer.