Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5884e66

Browse files
committed
Add importsdpa and exportsdpa
1 parent16ceda4 commit5884e66

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

‎sdpap/fileio.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
December 2010: Modified for SciPy
2323
"""
2424

25-
__all__= ['readproblem','writeproblem','fromsdpa','tosdpa']
25+
__all__= ['readproblem','writeproblem','fromsdpa','tosdpa','importsdpa','exportsdpa']
2626

2727
from .importconvert
2828
from .symconeimportSymCone
2929
fromscipy.sparseimportcsc_matrix,csr_matrix
3030
fromscipyimportsparse
31+
importwarnings
3132

3233

3334
defreadproblem(filename):
@@ -267,7 +268,7 @@ def writeproblem(filename, A, b, c, K, J, accuracy="%+8.16e"):
267268
return
268269

269270

270-
deffromsdpa(filename):
271+
defimportsdpa(filename,flipsign=True):
271272
"""Convert from SDPA sparse format to CLP format
272273
273274
Args:
@@ -452,10 +453,15 @@ def fromsdpa(filename):
452453
A=csc_matrix((elements_A[2], (elements_A[0],elements_A[1])),
453454
shape=(J.f,size_K))
454455

456+
ifflipsign:
457+
c=-c
458+
b=-b
459+
A=-A
460+
455461
returnA,b,c,K,J
456462

457463

458-
deftosdpa(filename,A,b,c,K,J,accuracy="%+8.16e"):
464+
defexportsdpa(filename,A,b,c,K,J,accuracy="%+8.16e",flipsign=True):
459465
"""Convert from SeDuMi format to SDPA sparse format
460466
461467
If J.l or J.q or J.s are not empty, CLP format will be converted
@@ -489,11 +495,14 @@ def tosdpa(filename, A, b, c, K, J, accuracy="%+8.16e"):
489495
# Note that primal-dual is reverse in SeDuMi.
490496
# So c2 must be -c2.
491497
# In addition, A2 should be passed in the transposed style.
492-
# December 2023 edit: to maintain consistency with `writeproblem` method
493-
# (that exports to CLP format) this sign flipping is being omitted here.
494-
c2=c
495-
b2=b
496-
A2=A
498+
ifflipsign:
499+
c2=-c
500+
b2=-b
501+
A2=-A
502+
else:
503+
c2=c
504+
b2=b
505+
A2=A
497506

498507
fp=open(filename,"w")
499508

@@ -670,6 +679,30 @@ def tosdpa(filename, A, b, c, K, J, accuracy="%+8.16e"):
670679
return
671680

672681

682+
# retaining them for backward compatibility
683+
684+
deffromsdpa(filename):
685+
warnings.warn("fromsdpa will be removed in the future. "
686+
"Please use importsdpa.",DeprecationWarning,stacklevel=2)
687+
A,b,c,K,J=importsdpa(filename,flipsign=False)
688+
returnA,b,c,K,J
689+
690+
deftosdpa(filename,A,b,c,K,J,accuracy="%+8.16e"):
691+
warnings.warn("tosdpa will be removed in the future. "
692+
"Please use exportsdpa.",DeprecationWarning,stacklevel=2)
693+
694+
# Note that with flipsign=True, this is identical to the exportsdpa routine
695+
# that replaces it (i.e. does not require flipping sign of A, b, c).
696+
exportsdpa(filename,A,b,c,K,J,accuracy="%+8.16e",flipsign=True)
697+
698+
# Reason for deprecating this (and the accompanying fromsdpa routine):
699+
# tosdpa did not require sign flipping (while fromsdpa required manual
700+
# sign flipping). This was confusing to the user.
701+
702+
# The new importsdpa and exportsdpa routines both perform sign
703+
# flipping by default which is intuitive to most users.
704+
705+
673706
# ==================================================
674707
# Functions to write result file
675708
# ==================================================

‎tests/test_fileio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def check_sdpa_consistency(reference_filename, accuracy="%+8.16e"):
5050
os.mkdir(output_dir)
5151
output_src=os.path.join(output_dir,reference_filename)
5252

53-
A,b,c,K,J=sdpap.fromsdpa(reference_src)
54-
sdpap.tosdpa(output_src,A,b,c,K,J,accuracy)
55-
A2,b2,c2,K2,J2=sdpap.fromsdpa(output_src)
53+
A,b,c,K,J=sdpap.importsdpa(reference_src)
54+
sdpap.exportsdpa(output_src,A,b,c,K,J,accuracy)
55+
A2,b2,c2,K2,J2=sdpap.importsdpa(output_src)
5656

5757
diff_A=abs(A2-A).max()
5858
diff_B=abs(b2-b).max()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp