abstract-math 0.0.0.21__py3-none-any.whl → 0.0.0.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of abstract-math might be problematic. Click here for more details.

@@ -1,5 +1,7 @@
1
1
  from decimal import Decimal, getcontext
2
2
  from .safe_math import *
3
+ def exponentials(value,exp=9,num=-1):
4
+ return multiply_it(value,exp_it(10,exp,num))
3
5
 
4
6
  # High precision for decimal ops
5
7
  getcontext().prec = 50
@@ -1,56 +1,44 @@
1
1
  from abstract_utilities import *
2
2
  import math
3
- #math functions ------------------------------------------------------------------------------------------------------
4
-
5
- def add_it(number_1,number_2):
6
- if return_0(number_1,number_2)==float(0):
7
- return float(0)
8
- return float(number_1)+float(number_2)
9
-
10
- def subtract_it(number_1,number_2):
11
- if return_0(number_1,number_2)==float(0):
12
- return float(0)
13
- return float(number_1)-float(number_2)
14
-
15
- def get_percentage(owner_balance,address_balance):
16
- retained_div = divide_it(owner_balance,address_balance)
17
- retained_mul = multiply_it(retained_div,100)
18
- return round(retained_mul,2)
19
-
20
- def return_0(*args):
21
- """Return True if *any* arg is “falsy” (None, non-number, or zero‐like)."""
22
- for arg in args:
23
- if arg is None or not is_number(arg) or str(arg).strip().lower() in ('0', '', 'null'):
24
- return True
25
- return False
3
+ import operator
4
+ from functools import reduce
5
+
6
+ # -------------------------------
7
+ # Core numeric hygiene
8
+ # -------------------------------
9
+ def _is_bad(x):
10
+ return (x is None) or (not is_number(x)) or (str(x).strip().lower() in ('', 'null'))
26
11
 
27
12
  def gather_0(*args):
28
- """Convert any “falsy” arg into float(0), leave others alone."""
29
- cleaned = []
30
- for arg in args:
31
- if arg is None or not is_number(arg) or str(arg).strip().lower() in ('0', '', 'null'):
32
- cleaned.append(0.0)
33
- else:
34
- cleaned.append(float(arg))
35
- return cleaned
13
+ """Convert any bad / empty / 'null' to 0.0, keep others as float."""
14
+ out = []
15
+ for a in args:
16
+ out.append(0.0 if _is_bad(a) else float(a))
17
+ return out
36
18
 
19
+ # -------------------------------
20
+ # Basic ops (single definitions)
21
+ # -------------------------------
37
22
  def add_it(*args):
38
- """Sum together any number of args, treating bad values as 0."""
23
+ """Sum all args; bad values become 0."""
24
+ return float(sum(gather_0(*args)))
25
+
26
+ def subtract_it(*args):
27
+ """a0 - a1 - a2 - ... ; bad values become 0."""
39
28
  nums = gather_0(*args)
40
- return float(sum(nums))
29
+ if not nums:
30
+ return 0.0
31
+ return float(reduce(operator.sub, nums))
41
32
 
42
33
  def multiply_it(*args):
43
- """Multiply any number of args, but return 0 if any input is bad or zero-like."""
34
+ """Multiply all args; if any becomes 0 (bad or literal zero), result is 0."""
44
35
  nums = gather_0(*args)
45
36
  if any(n == 0.0 for n in nums):
46
37
  return 0.0
47
38
  return float(reduce(operator.mul, nums, 1.0))
48
39
 
49
40
  def divide_it(*args):
50
- """
51
- Divide args[0] by args[1] by args[2]…
52
- If any input is bad or you hit a zero‐division, return 0.
53
- """
41
+ """a0 / a1 / a2 / ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
54
42
  nums = gather_0(*args)
55
43
  try:
56
44
  return float(reduce(operator.truediv, nums))
@@ -58,106 +46,162 @@ def divide_it(*args):
58
46
  return 0.0
59
47
 
60
48
  def floor_divide_it(*args):
61
- """
62
- Floor‐divide args[0] by args[1] by args[2]…
63
- If any input is bad or you hit a zero‐division, return 0.
64
- """
49
+ """a0 // a1 // a2 // ... ; bad => 0, safe against ZeroDivisionError (returns 0)."""
65
50
  nums = gather_0(*args)
66
51
  try:
67
52
  return float(reduce(operator.floordiv, nums))
68
53
  except ZeroDivisionError:
69
54
  return 0.0
70
55
 
71
- def subtract_it(*args):
72
- """
73
- Subtract all subsequent args from the first:
74
- args[0] - args[1] - args[2] - …
75
- Bad values become 0.
76
- """
77
- nums = gather_0(*args)
78
- if not nums:
79
- return 0.0
80
- return float(reduce(operator.sub, nums))
81
-
56
+ # -------------------------------
57
+ # Exponent helpers (deduplicated)
58
+ # -------------------------------
82
59
  def exp_it(base, *factors):
83
60
  """
84
61
  Raise `base` to the power of (product of all `factors`).
85
- e.g. exp_it(10, 2, 3) 10**(2*3) == 10**6
86
- Bad values become 0 (and so the result is 0).
62
+ exp_it(10, 2, 3) -> 10 ** (2*3) == 10**6
63
+ If any input is bad or no factors given, returns 0.0 (consistent with other helpers).
87
64
  """
88
65
  nums = gather_0(base, *factors)
89
66
  b, *f = nums
90
- if return_0(*nums) or not f:
67
+ if not f or any(n == 0.0 for n in nums):
91
68
  return 0.0
92
- exponent = reduce(operator.mul, f, 1.0)
69
+ exponent = float(reduce(operator.mul, f, 1.0))
93
70
  return float(b) ** exponent
94
71
 
72
+ def pow10(exp):
73
+ """10**exp with float output; bad exp -> 0.0^? -> return 1.0 (neutral for scaling)."""
74
+ if _is_bad(exp):
75
+ return 1.0
76
+ return 10.0 ** float(exp)
77
+
78
+ def scale_pow10(value, exp):
79
+ """Scale value by 10**exp. Neutral if exp is bad."""
80
+ return multiply_it(value, pow10(exp))
81
+
82
+ # Canonical public API for “multiply by 10^…”:
95
83
  def exponential(value, *exp_factors):
96
84
  """
97
- Multiply `value` by 10 to the power of (product of exp_factors).
98
- e.g. exponential(5, 2, 3) 5 * (10**(2*3)) == 5 * 1_000_000
99
- If you give no exp_factors, it just returns value.
85
+ Multiply `value` by 10 ** (product(exp_factors)).
86
+ exponential(5, 2, 3) -> 5 * 10**(2*3) == 5e6
87
+ If no exp_factors are given, returns value.
88
+ """
89
+ if not exp_factors:
90
+ return float(value) if not _is_bad(value) else 0.0
91
+ exp_prod = multiply_it(*exp_factors)
92
+ if exp_prod == 0.0 and all(not _is_bad(x) for x in exp_factors):
93
+ # valid inputs produced 0 exponent => scale by 1
94
+ return float(value) if not _is_bad(value) else 0.0
95
+ return scale_pow10(value, exp_prod)
96
+
97
+ # Back-compat alias for legacy callers:
98
+ def exponentials(value, exp=9, num=1):
99
+ """
100
+ Legacy alias: multiply `value` by 10 ** (exp * num).
101
+ Prefer: exponential(value, exp, num) or scale_pow10(value, exp*num).
100
102
  """
101
- power = exp_it(10, *exp_factors) or 1.0
102
- return multiply_it(value, power)
103
+ return exponential(value, exp, num)
103
104
 
104
- def get_proper_args(strings,*args,**kwargs):
105
- properArgs = []
105
+ # -------------------------------
106
+ # Argument picker
107
+ # -------------------------------
108
+ def get_proper_args(strings, *args, **kwargs):
109
+ """
110
+ Extract values by key order from kwargs first; if missing, consume positional args in order.
111
+ Example: get_proper_args(["a","b"], 1, b=3) -> [1, 3]
112
+ """
113
+ proper = []
114
+ args = list(args)
106
115
  for key in strings:
107
- kwarg = kwargs.get(key)
108
- if kwarg == None and args:
109
- kwarg = args[0]
110
- args = [] if len(args) == 1 else args[1:]
111
- properArgs.append(kwarg)
112
- return properArgs
113
- def get_lamp_difference(*args,**kwargs):
114
- sol_lamports =int(exponential(1,exp=9,num=1))
115
- proper_args = get_proper_args(["virtualSolReserves"],*args,**kwargs)
116
- virtualLamports = len(str(proper_args[0]))
117
- virtual_sol_lamports =int(exponential(1,exp=virtualLamports,num=1))
118
- return int(exponential(1,exp=len(str(int(virtual_sol_lamports/sol_lamports))),num=1))
119
- def get_price(*args,**kwargs):
120
- proper_args = get_proper_args(["virtualSolReserves","virtualTokenReserves"],*args,**kwargs)
121
- return divide_it(*proper_args)/get_lamp_difference(*args,**kwargs)
122
- def get_amount_price(*args,**kwargs):
123
- proper_args = get_proper_args(["solAmount","tokenAmount"],*args,**kwargs)
124
- return divide_it(*proper_args)
125
- def getSolAmountUi(*args,**kwargs):
126
- proper_args = get_proper_args(["solAmount"],*args,**kwargs)
127
- return exponential(proper_args[0],9)
128
- def getTokenAmountUi(*args,**kwargs):
129
- solAmountUi = getSolAmountUi(*args,**kwargs)
130
- price = get_price(*args,**kwargs)
131
- return solAmountUi/price
132
- def derive_token_decimals(*args,**kwargs):
133
- proper_args = get_proper_args(["virtualTokenReserves","tokenAmount"],*args,**kwargs)
134
- price = get_price(*args,**kwargs)
135
- if not (proper_args[1] > 0 and proper_args[0] > 0 and price > 0):
116
+ if key in kwargs:
117
+ proper.append(kwargs[key])
118
+ elif args:
119
+ proper.append(args.pop(0))
120
+ else:
121
+ proper.append(None)
122
+ return proper
123
+
124
+ # -------------------------------
125
+ # Your SOL / token helpers (using the unified expo)
126
+ # -------------------------------
127
+ SOL_DECIMAL_PLACE = 9
128
+ SOL_LAMPORTS = int(pow10(SOL_DECIMAL_PLACE)) # 10**9
129
+
130
+ def get_lamp_difference(*args, **kwargs):
131
+ """
132
+ Keep behavior: compute a lamport “scale” based on digit length of virtualSolReserves.
133
+ """
134
+ sol_lamports = SOL_LAMPORTS
135
+ [virtualSolReserves] = get_proper_args(["virtualSolReserves"], *args, **kwargs)
136
+ if _is_bad(virtualSolReserves):
137
+ return 0
138
+ virtual_len = len(str(int(float(virtualSolReserves))))
139
+ virtual_sol_lamports = int(pow10(virtual_len)) # 10**virtual_len
140
+ scale_len = len(str(int(virtual_sol_lamports / sol_lamports))) if sol_lamports else 0
141
+ return int(pow10(scale_len))
142
+
143
+ def get_price(*args, **kwargs):
144
+ """
145
+ price = virtualSolReserves / virtualTokenReserves / lamp_difference
146
+ """
147
+ virtualSolReserves, virtualTokenReserves = get_proper_args(
148
+ ["virtualSolReserves", "virtualTokenReserves"], *args, **kwargs
149
+ )
150
+ base = divide_it(virtualSolReserves, virtualTokenReserves)
151
+ return divide_it(base, get_lamp_difference(*args, **kwargs))
152
+
153
+ def get_amount_price(*args, **kwargs):
154
+ solAmount, tokenAmount = get_proper_args(["solAmount", "tokenAmount"], *args, **kwargs)
155
+ return divide_it(solAmount, tokenAmount)
156
+
157
+ def getSolAmountUi(*args, **kwargs):
158
+ [solAmount] = get_proper_args(["solAmount"], *args, **kwargs)
159
+ # scale by 10**9
160
+ return exponential(solAmount, SOL_DECIMAL_PLACE)
161
+
162
+ def getTokenAmountUi(*args, **kwargs):
163
+ solAmountUi = getSolAmountUi(*args, **kwargs)
164
+ price = get_price(*args, **kwargs)
165
+ return divide_it(solAmountUi, price)
166
+
167
+ def derive_token_decimals(*args, **kwargs):
168
+ virtualTokenReserves, tokenAmount = get_proper_args(
169
+ ["virtualTokenReserves", "tokenAmount"], *args, **kwargs
170
+ )
171
+ price = get_price(*args, **kwargs)
172
+ if not (virtualTokenReserves and tokenAmount and price) or any(
173
+ float(x) <= 0 for x in [virtualTokenReserves, tokenAmount, price]
174
+ ):
136
175
  raise ValueError("All inputs must be positive.")
137
- derived_token_amount = proper_args[0] / price
138
- ratio = derived_token_amount / proper_args[1]
176
+ derived_token_amount = divide_it(virtualTokenReserves, price)
177
+ ratio = divide_it(derived_token_amount, tokenAmount)
178
+ # count decimal places needed to make ratio an integer (tolerant to fp noise)
139
179
  decimals = -1
140
180
  while abs(ratio - round(ratio)) > 1e-9:
141
181
  ratio *= 10
142
182
  decimals += 1
143
183
  return decimals
184
+
144
185
  def derive_token_decimals_from_token_variables(variables):
145
- variables["price"] = get_price(**variables)
146
- derived_token_amount = variables["virtualTokenReserves"] / variables["price"]
147
- ratio = derived_token_amount / variables["tokenAmount"]
148
- decimals = -1
149
- while abs(ratio - round(ratio)) > 1e-9:
150
- ratio *= 10
151
- decimals += 1
152
- variables["tokenDecimals"] = decimals
153
- return variables
154
- def get_token_amount_ui(*args,**kwargs):
155
- proper_args = get_proper_args(["tokenAmount"],*args,**kwargs)
156
- return exponential(proper_args[0],exp=-derive_token_decimals(*args,**kwargs),num=1)
186
+ variables["price"] = get_price(**variables)
187
+ derived_token_amount = divide_it(variables["virtualTokenReserves"], variables["price"])
188
+ ratio = divide_it(derived_token_amount, variables["tokenAmount"])
189
+ decimals = -1
190
+ while abs(ratio - round(ratio)) > 1e-9:
191
+ ratio *= 10
192
+ decimals += 1
193
+ variables["tokenDecimals"] = decimals
194
+ return variables
195
+
196
+ def get_token_amount_ui(*args, **kwargs):
197
+ [tokenAmount] = get_proper_args(["tokenAmount"], *args, **kwargs)
198
+ # scale by 10**(-token_decimals)
199
+ dec = derive_token_decimals(*args, **kwargs)
200
+ return exponential(tokenAmount, -dec, 1)
201
+
157
202
  def update_token_variables(variables):
158
203
  variables['solAmountUi'] = getSolAmountUi(**variables)
159
- variables['solDecimals'] = 9
204
+ variables['solDecimals'] = SOL_DECIMAL_PLACE
160
205
  variables = derive_token_decimals_from_token_variables(variables)
161
- variables['tokenAmountUi'] = exponential(variables['tokenAmount'],exp=-variables["tokenDecimals"],num=1)
206
+ variables['tokenAmountUi'] = exponential(variables['tokenAmount'], -variables["tokenDecimals"], 1)
162
207
  return variables
163
-
@@ -1,5 +1,5 @@
1
1
  import math
2
2
  from typing import Dict, Callable, Optional, Any
3
- from ....safe_math import (
3
+ from ...safe_math import (
4
4
  multiply_it as mul, divide_it as div, add_it as add, subtract_it as sub, exp_it as exp
5
5
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: abstract_math
3
- Version: 0.0.0.21
3
+ Version: 0.0.0.23
4
4
  Author: putkoff
5
5
  Author-email: partners@abstractendeavors.com
6
6
  Classifier: Development Status :: 3 - Alpha
@@ -1,13 +1,13 @@
1
1
  abstract_math/__init__.py,sha256=wr-kwaB8micSsR1nCHuDXRlsL59DWlqZQFN7M_rfZys,80
2
- abstract_math/derive_tokens.py,sha256=Ts_Jh-UnVQEgo3cfkIhpbaXGPmLG148pGqksnuOtJxk,4498
3
- abstract_math/safe_math.py,sha256=sJvGIMj2urcsAeWcc72WBjzrpkaKIRJoZUBYdaC9tzA,5961
2
+ abstract_math/derive_tokens.py,sha256=mVzwkLv1Jo76R-SGR0hZbO5MPOr7vZpDC44eyxUvfzU,4585
3
+ abstract_math/safe_math.py,sha256=9T1-n6dcMYzJtem8xEIGEKOLR5X4DOIj_h9aotvyzdE,7347
4
4
  abstract_math/flask_scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  abstract_math/flask_scripts/flask_utils.py,sha256=z6gpPshiOeqNANBQkZMZPkegCzj-GK3OpSOi1lN19Sc,28483
6
6
  abstract_math/solar_math/__init__.py,sha256=FfPkmWi3cQ3BLvUUUf6k22rGBE8scDL1EoM5l24biVo,39
7
7
  abstract_math/solar_math/flask_utils.py,sha256=c82LrZ0dKC3Y8LxqOs1zDR6gRy8oXSx9balBD18gNB0,459
8
8
  abstract_math/solar_math/main.py,sha256=EtcUmUWj27c7mcnm-hlfeCeYFuPytPevXx5vClVu6Q8,10000
9
9
  abstract_math/solar_math/src/__init__.py,sha256=qeal73F-NLu6sO_YSmD_komVpe17f0OFy2IfBR2QU_s,70
10
- abstract_math/solar_math/src/imports.py,sha256=GfkWfXDc6fzLe7kHYlSdghkN0Nob0xApxhEyRyDcZS0,182
10
+ abstract_math/solar_math/src/imports.py,sha256=UZp2SkF8INamFVwLOIca6w9K-YKXsqhiqcf6bkWuLVo,181
11
11
  abstract_math/solar_math/src/constants/__init__.py,sha256=HpRa4MWkJZu3Wcx6KchB8__RL4FfkVqRw78TAnjRRwo,691
12
12
  abstract_math/solar_math/src/constants/distance_constants.py,sha256=stM3nWxpANQEtoxFcbxpO0ly_UM4JJ2aH0VK42fHw6s,1985
13
13
  abstract_math/solar_math/src/constants/planet_constants.py,sha256=tt2nbFtRtYhUU9D918WK91D-S5Cv0HEJ-1j7f9Yb50w,6602
@@ -16,7 +16,7 @@ abstract_math/solar_math/src/utils/__init__.py,sha256=Vb2bfx1p9YSmhnbqXBjGVPt1OQ
16
16
  abstract_math/solar_math/src/utils/escape_velocity.py,sha256=9oixNxWyY4pxa7e1INwbreD8GJEw2BZIgV_IqXiDd9Y,2971
17
17
  abstract_math/solar_math/src/utils/geometry_utils.py,sha256=Ij4mASNFp2-CWy425fxMTEuMPuAZHs-E0qvi3BjaNkk,4169
18
18
  abstract_math/solar_math/src/utils/velocity_utils.py,sha256=_SyOscVvYsvbm1T1Rh4uqegXdf61mHll5s3UfQcdUnU,1162
19
- abstract_math-0.0.0.21.dist-info/METADATA,sha256=YkC0CIK7ZbdbL-TkobbbEaIjonlma317OySbl8ShCmE,3081
20
- abstract_math-0.0.0.21.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
- abstract_math-0.0.0.21.dist-info/top_level.txt,sha256=b7jOgD9c0U-CGH-l7yxhMPukzD40kMEQkQRV_sGyVfE,14
22
- abstract_math-0.0.0.21.dist-info/RECORD,,
19
+ abstract_math-0.0.0.23.dist-info/METADATA,sha256=sE9PH3BoVK35D72oQm4Le2vgRqxmsYxYMcqKz9VuZOU,3081
20
+ abstract_math-0.0.0.23.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ abstract_math-0.0.0.23.dist-info/top_level.txt,sha256=b7jOgD9c0U-CGH-l7yxhMPukzD40kMEQkQRV_sGyVfE,14
22
+ abstract_math-0.0.0.23.dist-info/RECORD,,