Skip to content

Commit 48039da

Browse files
committed
slightly improve csd
1 parent d54f7d9 commit 48039da

File tree

2 files changed

+62
-56
lines changed

2 files changed

+62
-56
lines changed

README.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
ellpy
2-
=====
1+
# ellpy
32

43
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/luk036/ellpy)
54
![Python application](https://github.com/luk036/ellpy/workflows/Python%20application/badge.svg)
@@ -14,29 +13,28 @@ ellpy
1413

1514
ellipsoid method python code
1615

17-
Features
18-
--------
16+
## Features
1917

2018
- At most one square-root per iteration.
2119
- Include oracles for Matrix Inequalities and Network problems.
2220
- Suport Parallel-Cuts.
2321

24-
Installation
25-
------------
22+
## Installation
2623

2724
- The core ellipsoid method depends only on the `numpy` module.
2825
- The network related oracles depends further on `networkx` and
2926
`luk036/netoptim`.
3027
- Some unit tests depends further on `numexpr` and `luk036/pylds`.
3128

32-
See also
33-
--------
29+
## See also
3430

3531
- [ellcpp](https://github.com/luk036/ellcpp)
3632
- [Presentation Slides](https://luk036.github.io/cvx)
3733

38-
Note
39-
----
4034

41-
This project has been set up using PyScaffold 3.2.1. For details and usage
42-
information on PyScaffold see <https://pyscaffold.org/>.
35+
<!-- pyscaffold-notes -->
36+
37+
## Note
38+
39+
This project has been set up using PyScaffold 4.0.2. For details and usage
40+
information on PyScaffold see https://pyscaffold.org/.

src/pycsd/csd/csd.py

+52-44
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,43 @@ def to_csd(num, places=0, debug=False):
2323
""" Convert the argument to CSD Format. """
2424

2525
if debug:
26-
print("Converting %f " % (num), )
26+
print(
27+
"Converting %f " % (num),
28+
)
2729

2830
# figure out binary range, special case for 0
29-
if num == 0.:
30-
return '0'
31+
if num == 0:
32+
return "0"
3133

3234
absnum = fabs(num)
33-
n = 0 if absnum < 1. else ceil(log(absnum * 1.5, 2))
34-
csd_str = '0' if absnum < 1. else ''
35+
n = 0 if absnum < 1.0 else ceil(log(absnum * 1.5, 2))
36+
csd_str = "0" if absnum < 1.0 else ""
3537

3638
if debug:
3739
print("to %d.%d format" % (n, places))
3840

39-
limit = pow(2., n) / 3.
40-
while (n > -places):
41+
# limit = pow(2., n) / 3.
42+
pow2n = pow(2.0, n - 1)
43+
while n > -places:
4144
if debug:
42-
print(" ", num, limit)
45+
print(" ", num, 2 * pow2n / 3)
4346

4447
# decimal point?
4548
if n == 0: # unlikely
46-
csd_str += '.'
49+
csd_str += "."
4750

4851
n -= 1
4952
# convert the number
50-
if num > limit:
51-
csd_str += '+'
52-
num -= 1.5 * limit
53-
elif num < -limit:
54-
csd_str += '-'
55-
num += 1.5 * limit
53+
d = 1.5 * num
54+
if d > pow2n:
55+
csd_str += "+"
56+
num -= pow2n
57+
elif d < -pow2n:
58+
csd_str += "-"
59+
num += pow2n
5660
else:
57-
csd_str += '0'
58-
limit /= 2.
61+
csd_str += "0"
62+
pow2n /= 2
5963

6064
if debug:
6165
print(csd_str)
@@ -69,23 +73,23 @@ def to_decimal(csd_str, debug=False):
6973
if debug:
7074
print("Converting: ", csd_str)
7175

72-
num = 0.
76+
num = 0.0
7377
loc = 0
7478
for i, c in enumerate(csd_str):
75-
num *= 2.
76-
if c == '+':
77-
num += 1.
78-
elif c == '-':
79-
num -= 1.
80-
elif c == '0':
79+
num *= 2.0
80+
if c == "+":
81+
num += 1.0
82+
elif c == "-":
83+
num -= 1.0
84+
elif c == "0":
8185
pass
82-
elif c == '.': # unlikely
83-
num /= 2.
86+
elif c == ".": # unlikely
87+
num /= 2.0
8488
loc = i + 1
8589
else:
8690
raise ValueError
8791
if loc != 0:
88-
num /= pow(2., len(csd_str) - loc)
92+
num /= pow(2.0, len(csd_str) - loc)
8993
return num
9094

9195

@@ -127,36 +131,40 @@ def to_csdfixed(num, nnz=4, debug=False):
127131
""" Convert the argument to CSD Format. """
128132

129133
if debug:
130-
print("Converting %f " % (num), )
134+
print(
135+
"Converting %f " % (num),
136+
)
131137

132138
# figure out binary range, special case for 0
133-
if num == 0.:
134-
return '0'
139+
if num == 0.0:
140+
return "0"
135141
absnum = fabs(num)
136-
n = 0 if absnum < 1. else ceil(log(absnum * 1.5, 2))
137-
csd_str = '0' if absnum < 1. else ''
138-
limit = pow(2., n) / 3.
139-
while (n > 0 or nnz > 0):
142+
n = 0 if absnum < 1.0 else ceil(log(absnum * 1.5, 2))
143+
csd_str = "0" if absnum < 1.0 else ""
144+
# limit = pow(2., n) / 3.
145+
pow2n = pow(2.0, n - 1)
146+
while n > 0 or nnz > 0:
140147
if debug:
141-
print(" ", num, limit)
148+
print(" ", num, 2 * pow2n / 3)
142149

143150
# decimal point?
144151
if n == 0:
145-
csd_str += '.'
152+
csd_str += "."
146153

147154
n -= 1
148155
# convert the number
149-
if num > limit:
150-
csd_str += '+'
151-
num -= 1.5 * limit
156+
d = 1.5 * num
157+
if d > pow2n:
158+
csd_str += "+"
159+
num -= pow2n
152160
nnz -= 1
153-
elif num < -limit:
154-
csd_str += '-'
155-
num += 1.5 * limit
161+
elif d < -pow2n:
162+
csd_str += "-"
163+
num += pow2n
156164
nnz -= 1
157165
else:
158-
csd_str += '0'
159-
limit /= 2.
166+
csd_str += "0"
167+
pow2n /= 2.0
160168

161169
if nnz == 0:
162170
num = 0

0 commit comments

Comments
 (0)