jijmodeling 0.10.15__cp38-cp38-macosx_11_0_x86_64.whl → 0.10.16__cp38-cp38-macosx_11_0_x86_64.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 jijmodeling might be problematic. Click here for more details.

Files changed (179) hide show
  1. jijmodeling/__init__.py +2 -0
  2. jijmodeling/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  3. jijmodeling/__pycache__/__init__.cpython-38.pyc +0 -0
  4. jijmodeling/__pycache__/_version.cpython-38.opt-1.pyc +0 -0
  5. jijmodeling/__pycache__/_version.cpython-38.pyc +0 -0
  6. jijmodeling/_version.py +2 -2
  7. jijmodeling/exceptions/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  8. jijmodeling/exceptions/__pycache__/__init__.cpython-38.pyc +0 -0
  9. jijmodeling/exceptions/__pycache__/exceptions.cpython-38.opt-1.pyc +0 -0
  10. jijmodeling/exceptions/__pycache__/exceptions.cpython-38.pyc +0 -0
  11. jijmodeling/expression/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  12. jijmodeling/expression/__pycache__/__init__.cpython-38.pyc +0 -0
  13. jijmodeling/expression/__pycache__/condition.cpython-38.opt-1.pyc +0 -0
  14. jijmodeling/expression/__pycache__/condition.cpython-38.pyc +0 -0
  15. jijmodeling/expression/__pycache__/constraint.cpython-38.opt-1.pyc +0 -0
  16. jijmodeling/expression/__pycache__/constraint.cpython-38.pyc +0 -0
  17. jijmodeling/expression/__pycache__/expression.cpython-38.opt-1.pyc +0 -0
  18. jijmodeling/expression/__pycache__/expression.cpython-38.pyc +0 -0
  19. jijmodeling/expression/__pycache__/extract.cpython-38.opt-1.pyc +0 -0
  20. jijmodeling/expression/__pycache__/extract.cpython-38.pyc +0 -0
  21. jijmodeling/expression/__pycache__/from_old_serializable.cpython-38.opt-1.pyc +0 -0
  22. jijmodeling/expression/__pycache__/from_old_serializable.cpython-38.pyc +0 -0
  23. jijmodeling/expression/__pycache__/mathfunc.cpython-38.opt-1.pyc +0 -0
  24. jijmodeling/expression/__pycache__/mathfunc.cpython-38.pyc +0 -0
  25. jijmodeling/expression/__pycache__/prod.cpython-38.opt-1.pyc +0 -0
  26. jijmodeling/expression/__pycache__/prod.cpython-38.pyc +0 -0
  27. jijmodeling/expression/__pycache__/serializable.cpython-38.opt-1.pyc +0 -0
  28. jijmodeling/expression/__pycache__/serializable.cpython-38.pyc +0 -0
  29. jijmodeling/expression/__pycache__/sum.cpython-38.opt-1.pyc +0 -0
  30. jijmodeling/expression/__pycache__/sum.cpython-38.pyc +0 -0
  31. jijmodeling/expression/__pycache__/type_annotations.cpython-38.opt-1.pyc +0 -0
  32. jijmodeling/expression/__pycache__/type_annotations.cpython-38.pyc +0 -0
  33. jijmodeling/expression/__pycache__/utils.cpython-38.opt-1.pyc +0 -0
  34. jijmodeling/expression/__pycache__/utils.cpython-38.pyc +0 -0
  35. jijmodeling/expression/utils.py +26 -0
  36. jijmodeling/expression/variables/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  37. jijmodeling/expression/variables/__pycache__/__init__.cpython-38.pyc +0 -0
  38. jijmodeling/expression/variables/__pycache__/deci_vars.cpython-38.opt-1.pyc +0 -0
  39. jijmodeling/expression/variables/__pycache__/deci_vars.cpython-38.pyc +0 -0
  40. jijmodeling/expression/variables/__pycache__/jagged_array.cpython-38.opt-1.pyc +0 -0
  41. jijmodeling/expression/variables/__pycache__/jagged_array.cpython-38.pyc +0 -0
  42. jijmodeling/expression/variables/__pycache__/placeholders.cpython-38.opt-1.pyc +0 -0
  43. jijmodeling/expression/variables/__pycache__/placeholders.cpython-38.pyc +0 -0
  44. jijmodeling/expression/variables/__pycache__/variable.cpython-38.opt-1.pyc +0 -0
  45. jijmodeling/expression/variables/__pycache__/variable.cpython-38.pyc +0 -0
  46. jijmodeling/latex_repr/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  47. jijmodeling/latex_repr/__pycache__/__init__.cpython-38.pyc +0 -0
  48. jijmodeling/latex_repr/__pycache__/latex_repr.cpython-38.opt-1.pyc +0 -0
  49. jijmodeling/latex_repr/__pycache__/latex_repr.cpython-38.pyc +0 -0
  50. jijmodeling/latex_repr/__pycache__/problem_latex_repr.cpython-38.opt-1.pyc +0 -0
  51. jijmodeling/latex_repr/__pycache__/problem_latex_repr.cpython-38.pyc +0 -0
  52. jijmodeling/match/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  53. jijmodeling/match/__pycache__/__init__.cpython-38.pyc +0 -0
  54. jijmodeling/match/__pycache__/condition_same.cpython-38.opt-1.pyc +0 -0
  55. jijmodeling/match/__pycache__/condition_same.cpython-38.pyc +0 -0
  56. jijmodeling/match/__pycache__/constraint_same.cpython-38.opt-1.pyc +0 -0
  57. jijmodeling/match/__pycache__/constraint_same.cpython-38.pyc +0 -0
  58. jijmodeling/match/__pycache__/expr_same.cpython-38.opt-1.pyc +0 -0
  59. jijmodeling/match/__pycache__/expr_same.cpython-38.pyc +0 -0
  60. jijmodeling/match/__pycache__/is_same_expr.cpython-38.opt-1.pyc +0 -0
  61. jijmodeling/match/__pycache__/is_same_expr.cpython-38.pyc +0 -0
  62. jijmodeling/match/__pycache__/penalty_same.cpython-38.opt-1.pyc +0 -0
  63. jijmodeling/match/__pycache__/penalty_same.cpython-38.pyc +0 -0
  64. jijmodeling/match/__pycache__/problem_same.cpython-38.opt-1.pyc +0 -0
  65. jijmodeling/match/__pycache__/problem_same.cpython-38.pyc +0 -0
  66. jijmodeling/match/__pycache__/replace.cpython-38.opt-1.pyc +0 -0
  67. jijmodeling/match/__pycache__/replace.cpython-38.pyc +0 -0
  68. jijmodeling/match/expr_same.py +9 -0
  69. jijmodeling/match/is_same_expr.py +17 -1
  70. jijmodeling/match/replace.py +210 -0
  71. jijmodeling/problem/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  72. jijmodeling/problem/__pycache__/__init__.cpython-38.pyc +0 -0
  73. jijmodeling/problem/__pycache__/problem.cpython-38.opt-1.pyc +0 -0
  74. jijmodeling/problem/__pycache__/problem.cpython-38.pyc +0 -0
  75. jijmodeling/protobuf/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  76. jijmodeling/protobuf/__pycache__/__init__.cpython-38.pyc +0 -0
  77. jijmodeling/protobuf/from_protobuf/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  78. jijmodeling/protobuf/from_protobuf/__pycache__/__init__.cpython-38.pyc +0 -0
  79. jijmodeling/protobuf/from_protobuf/__pycache__/extract_attribute_value_from_header.cpython-38.opt-1.pyc +0 -0
  80. jijmodeling/protobuf/from_protobuf/__pycache__/extract_attribute_value_from_header.cpython-38.pyc +0 -0
  81. jijmodeling/protobuf/from_protobuf/__pycache__/from_protobuf.cpython-38.opt-1.pyc +0 -0
  82. jijmodeling/protobuf/from_protobuf/__pycache__/from_protobuf.cpython-38.pyc +0 -0
  83. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_array_shape.cpython-38.opt-1.pyc +0 -0
  84. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_array_shape.cpython-38.pyc +0 -0
  85. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_binary_op.cpython-38.opt-1.pyc +0 -0
  86. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_binary_op.cpython-38.pyc +0 -0
  87. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_binary_var.cpython-38.opt-1.pyc +0 -0
  88. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_binary_var.cpython-38.pyc +0 -0
  89. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_constraint.cpython-38.opt-1.pyc +0 -0
  90. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_constraint.cpython-38.pyc +0 -0
  91. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_element.cpython-38.opt-1.pyc +0 -0
  92. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_element.cpython-38.pyc +0 -0
  93. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_expression.cpython-38.opt-1.pyc +0 -0
  94. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_expression.cpython-38.pyc +0 -0
  95. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_forall.cpython-38.opt-1.pyc +0 -0
  96. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_forall.cpython-38.pyc +0 -0
  97. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_integer_var.cpython-38.opt-1.pyc +0 -0
  98. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_integer_var.cpython-38.pyc +0 -0
  99. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_jagged_array.cpython-38.opt-1.pyc +0 -0
  100. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_jagged_array.cpython-38.pyc +0 -0
  101. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_number_lit.cpython-38.opt-1.pyc +0 -0
  102. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_number_lit.cpython-38.pyc +0 -0
  103. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_penalty.cpython-38.opt-1.pyc +0 -0
  104. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_penalty.cpython-38.pyc +0 -0
  105. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_placeholder.cpython-38.opt-1.pyc +0 -0
  106. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_placeholder.cpython-38.pyc +0 -0
  107. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_problem.cpython-38.opt-1.pyc +0 -0
  108. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_problem.cpython-38.pyc +0 -0
  109. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_reduction_op.cpython-38.opt-1.pyc +0 -0
  110. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_reduction_op.cpython-38.pyc +0 -0
  111. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_subscript_op.cpython-38.opt-1.pyc +0 -0
  112. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_subscript_op.cpython-38.pyc +0 -0
  113. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_unary_op.cpython-38.opt-1.pyc +0 -0
  114. jijmodeling/protobuf/from_protobuf/__pycache__/message_to_unary_op.cpython-38.pyc +0 -0
  115. jijmodeling/protobuf/to_protobuf/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  116. jijmodeling/protobuf/to_protobuf/__pycache__/__init__.cpython-38.pyc +0 -0
  117. jijmodeling/protobuf/to_protobuf/__pycache__/add_header_to_message.cpython-38.opt-1.pyc +0 -0
  118. jijmodeling/protobuf/to_protobuf/__pycache__/add_header_to_message.cpython-38.pyc +0 -0
  119. jijmodeling/protobuf/to_protobuf/__pycache__/array_shape_to_message.cpython-38.opt-1.pyc +0 -0
  120. jijmodeling/protobuf/to_protobuf/__pycache__/array_shape_to_message.cpython-38.pyc +0 -0
  121. jijmodeling/protobuf/to_protobuf/__pycache__/binary_op_to_message.cpython-38.opt-1.pyc +0 -0
  122. jijmodeling/protobuf/to_protobuf/__pycache__/binary_op_to_message.cpython-38.pyc +0 -0
  123. jijmodeling/protobuf/to_protobuf/__pycache__/binary_var_to_message.cpython-38.opt-1.pyc +0 -0
  124. jijmodeling/protobuf/to_protobuf/__pycache__/binary_var_to_message.cpython-38.pyc +0 -0
  125. jijmodeling/protobuf/to_protobuf/__pycache__/constraint_to_message.cpython-38.opt-1.pyc +0 -0
  126. jijmodeling/protobuf/to_protobuf/__pycache__/constraint_to_message.cpython-38.pyc +0 -0
  127. jijmodeling/protobuf/to_protobuf/__pycache__/element_to_message.cpython-38.opt-1.pyc +0 -0
  128. jijmodeling/protobuf/to_protobuf/__pycache__/element_to_message.cpython-38.pyc +0 -0
  129. jijmodeling/protobuf/to_protobuf/__pycache__/expression_to_message.cpython-38.opt-1.pyc +0 -0
  130. jijmodeling/protobuf/to_protobuf/__pycache__/expression_to_message.cpython-38.pyc +0 -0
  131. jijmodeling/protobuf/to_protobuf/__pycache__/forall_to_message.cpython-38.opt-1.pyc +0 -0
  132. jijmodeling/protobuf/to_protobuf/__pycache__/forall_to_message.cpython-38.pyc +0 -0
  133. jijmodeling/protobuf/to_protobuf/__pycache__/integer_var_to_message.cpython-38.opt-1.pyc +0 -0
  134. jijmodeling/protobuf/to_protobuf/__pycache__/integer_var_to_message.cpython-38.pyc +0 -0
  135. jijmodeling/protobuf/to_protobuf/__pycache__/jagged_array_to_message.cpython-38.opt-1.pyc +0 -0
  136. jijmodeling/protobuf/to_protobuf/__pycache__/jagged_array_to_message.cpython-38.pyc +0 -0
  137. jijmodeling/protobuf/to_protobuf/__pycache__/number_lit_to_message.cpython-38.opt-1.pyc +0 -0
  138. jijmodeling/protobuf/to_protobuf/__pycache__/number_lit_to_message.cpython-38.pyc +0 -0
  139. jijmodeling/protobuf/to_protobuf/__pycache__/penalty_to_message.cpython-38.opt-1.pyc +0 -0
  140. jijmodeling/protobuf/to_protobuf/__pycache__/penalty_to_message.cpython-38.pyc +0 -0
  141. jijmodeling/protobuf/to_protobuf/__pycache__/placeholder_to_message.cpython-38.opt-1.pyc +0 -0
  142. jijmodeling/protobuf/to_protobuf/__pycache__/placeholder_to_message.cpython-38.pyc +0 -0
  143. jijmodeling/protobuf/to_protobuf/__pycache__/problem_to_message.cpython-38.opt-1.pyc +0 -0
  144. jijmodeling/protobuf/to_protobuf/__pycache__/problem_to_message.cpython-38.pyc +0 -0
  145. jijmodeling/protobuf/to_protobuf/__pycache__/reduction_op_to_message.cpython-38.opt-1.pyc +0 -0
  146. jijmodeling/protobuf/to_protobuf/__pycache__/reduction_op_to_message.cpython-38.pyc +0 -0
  147. jijmodeling/protobuf/to_protobuf/__pycache__/subscript_op_to_message.cpython-38.opt-1.pyc +0 -0
  148. jijmodeling/protobuf/to_protobuf/__pycache__/subscript_op_to_message.cpython-38.pyc +0 -0
  149. jijmodeling/protobuf/to_protobuf/__pycache__/to_protobuf.cpython-38.opt-1.pyc +0 -0
  150. jijmodeling/protobuf/to_protobuf/__pycache__/to_protobuf.cpython-38.pyc +0 -0
  151. jijmodeling/protobuf/to_protobuf/__pycache__/unary_op_to_message.cpython-38.opt-1.pyc +0 -0
  152. jijmodeling/protobuf/to_protobuf/__pycache__/unary_op_to_message.cpython-38.pyc +0 -0
  153. jijmodeling/sampleset/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  154. jijmodeling/sampleset/__pycache__/__init__.cpython-38.pyc +0 -0
  155. jijmodeling/sampleset/__pycache__/evaluation.cpython-38.opt-1.pyc +0 -0
  156. jijmodeling/sampleset/__pycache__/evaluation.cpython-38.pyc +0 -0
  157. jijmodeling/sampleset/__pycache__/measuring_time.cpython-38.opt-1.pyc +0 -0
  158. jijmodeling/sampleset/__pycache__/measuring_time.cpython-38.pyc +0 -0
  159. jijmodeling/sampleset/__pycache__/record.cpython-38.opt-1.pyc +0 -0
  160. jijmodeling/sampleset/__pycache__/record.cpython-38.pyc +0 -0
  161. jijmodeling/sampleset/__pycache__/sampleset.cpython-38.opt-1.pyc +0 -0
  162. jijmodeling/sampleset/__pycache__/sampleset.cpython-38.pyc +0 -0
  163. jijmodeling/sampleset/__pycache__/solving_time.cpython-38.opt-1.pyc +0 -0
  164. jijmodeling/sampleset/__pycache__/solving_time.cpython-38.pyc +0 -0
  165. jijmodeling/sampleset/__pycache__/system_time.cpython-38.opt-1.pyc +0 -0
  166. jijmodeling/sampleset/__pycache__/system_time.cpython-38.pyc +0 -0
  167. jijmodeling/type_annotations/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  168. jijmodeling/type_annotations/__pycache__/__init__.cpython-38.pyc +0 -0
  169. jijmodeling/type_annotations/__pycache__/type_annotations.cpython-38.opt-1.pyc +0 -0
  170. jijmodeling/type_annotations/__pycache__/type_annotations.cpython-38.pyc +0 -0
  171. jijmodeling/utils/__pycache__/__init__.cpython-38.opt-1.pyc +0 -0
  172. jijmodeling/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  173. jijmodeling/utils/__pycache__/utils.cpython-38.opt-1.pyc +0 -0
  174. jijmodeling/utils/__pycache__/utils.cpython-38.pyc +0 -0
  175. {jijmodeling-0.10.15.dist-info → jijmodeling-0.10.16.dist-info}/METADATA +1 -1
  176. {jijmodeling-0.10.15.dist-info → jijmodeling-0.10.16.dist-info}/RECORD +179 -176
  177. {jijmodeling-0.10.15.dist-info → jijmodeling-0.10.16.dist-info}/LICENSE.txt +0 -0
  178. {jijmodeling-0.10.15.dist-info → jijmodeling-0.10.16.dist-info}/WHEEL +0 -0
  179. {jijmodeling-0.10.15.dist-info → jijmodeling-0.10.16.dist-info}/top_level.txt +0 -0
jijmodeling/__init__.py CHANGED
@@ -41,6 +41,7 @@ from jijmodeling.match.expr_same import expr_same
41
41
  from jijmodeling.match.is_same_expr import is_same_cond, is_same_expr
42
42
  from jijmodeling.match.penalty_same import penalty_same
43
43
  from jijmodeling.match.problem_same import problem_same
44
+ from jijmodeling.match.replace import replace
44
45
  from jijmodeling.problem.problem import Problem, ProblemSense
45
46
  from jijmodeling.protobuf.from_protobuf.from_protobuf import from_protobuf
46
47
  from jijmodeling.protobuf.to_protobuf.to_protobuf import to_protobuf
@@ -89,6 +90,7 @@ __all__ = [
89
90
  "is_same_expr",
90
91
  "penalty_same",
91
92
  "problem_same",
93
+ "replace",
92
94
  "Binary",
93
95
  "DecisionVariable",
94
96
  "Element",
jijmodeling/_version.py CHANGED
@@ -1,4 +1,4 @@
1
1
  # file generated by setuptools_scm
2
2
  # don't change, don't track in version control
3
- __version__ = version = '0.10.15'
4
- __version_tuple__ = version_tuple = (0, 10, 15)
3
+ __version__ = version = '0.10.16'
4
+ __version_tuple__ = version_tuple = (0, 10, 16)
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import itertools as it
3
4
  import typing as tp
4
5
 
5
6
  import typeguard as _typeguard
@@ -173,3 +174,28 @@ def check_non_decision_variable(
173
174
  for v in variables:
174
175
  if isinstance(v, _deci_vars.DecisionVariable):
175
176
  raise _exceptions.CannotContainDecisionVarError(error_msg)
177
+
178
+
179
+ def flatten_binary_operator(
180
+ expression: _expression.Expression,
181
+ op_type: tp.Type[_expression.BinaryOperator] = _expression.Add,
182
+ ) -> tp.List[_expression.Expression]:
183
+ """
184
+ Flatten `op_type` binary operator to list.
185
+ This function flattens a given node to the following:
186
+ node1 <> node2 <> node3 <> ... <> nodeN -> [node1, node2, node3, ..., nodeN]
187
+ where <> is a specified binary operator by `op_type`.
188
+
189
+ Args:
190
+ expression (Expression): target expression.
191
+ op_type (Type[BinaryOperator]): target binary operator.
192
+
193
+ Returns:
194
+ List[Expression]: list of expression. Each element is not binary operator.
195
+ """
196
+ if isinstance(expression, op_type):
197
+ left = flatten_binary_operator(expression.left, op_type)
198
+ right = flatten_binary_operator(expression.right, op_type)
199
+ return list(it.chain.from_iterable([left, right]))
200
+ else:
201
+ return [expression]
@@ -13,6 +13,15 @@ def expr_same(
13
13
  pattern: Union[ExpressionType, Condition],
14
14
  check_id: bool = True,
15
15
  ) -> bool:
16
+ """
17
+ Check if two expressions are the same.
18
+ Args:
19
+ target (Union[ExpressionType, Condition]): target expression
20
+ pattern (Union[ExpressionType, Condition]): pattern expression
21
+ check_id (bool): check if the uuid is the same, default is True
22
+ Returns:
23
+ bool: True if the expressions are the same
24
+ """
16
25
  if isinstance(target, (Expression, Range)) and isinstance(
17
26
  pattern, (Expression, Range)
18
27
  ):
@@ -31,6 +31,8 @@ def is_same_expr(
31
31
  # Check if the type of `target` is a subclass of the type of `pattern`
32
32
  if not isinstance(target, type(pattern)):
33
33
  return False
34
+ if type(target) != type(pattern):
35
+ return False
34
36
 
35
37
  # Case: Number
36
38
  if type(target) is Number and type(pattern) is Number:
@@ -70,9 +72,11 @@ def is_same_expr(
70
72
  pattern, ReductionOperator
71
73
  ):
72
74
  return is_same_reduction_op(target, pattern, check_id)
75
+ elif type(target) is ArrayShape and type(pattern) is ArrayShape:
76
+ return is_same_arrayshape(target, pattern, check_id)
73
77
  # Case: No match
74
78
  else:
75
- raise TypeError(f"{target.__class__.__name__} is not supported.")
79
+ raise TypeError(f"{target.__class__.__name__} is not supported in `is_same_expr`.")
76
80
 
77
81
 
78
82
  def is_same_cond(target: Condition, pattern: Condition, check_id) -> bool:
@@ -112,6 +116,18 @@ def is_same_number(target: Number, pattern: Number) -> bool:
112
116
  return True
113
117
 
114
118
 
119
+ def is_same_arrayshape(target: ArrayShape, pattern: ArrayShape, check_id: bool) -> bool:
120
+ # Compare the following attributes:
121
+ # - array
122
+ # - dim
123
+ if is_same_expr(target.array, pattern.array, check_id):
124
+ return False
125
+ elif target.dimension != pattern.dimension:
126
+ return False
127
+ else:
128
+ return True
129
+
130
+
115
131
  def is_same_shape(target: ShapeType, pattern: ShapeType, check_id: bool) -> bool:
116
132
  if len(target) != len(pattern):
117
133
  return False
@@ -0,0 +1,210 @@
1
+ import typing as tp
2
+
3
+ from functools import singledispatch
4
+
5
+ import jijmodeling as jm
6
+ from jijmodeling.exceptions.exceptions import JijModelingError
7
+ import jijmodeling.expression.condition as jc
8
+
9
+ from jijmodeling.match.expr_same import expr_same
10
+
11
+
12
+ def replace(
13
+ expr: jm.Expression, target: jm.Expression, source: jm.Expression, check_id: bool
14
+ ) -> jm.Expression:
15
+ """
16
+ replace expression (from `target` element to `source` one)
17
+ This function selects the target element from the expression and replaces it with the source element.
18
+
19
+ Args:
20
+ expr (jm.Expression): expression
21
+ target (jm.Expression): target sub tree
22
+ source (jm.Expression): new sub tree
23
+ check_id (bool): replace only if the uuid is the same, default is True
24
+
25
+ Returns:
26
+ jm.Expression: replaced sub tree
27
+
28
+ Examples:
29
+ >>> import jijmodeling as jm
30
+ >>> n = jm.Placeholder("n")
31
+ >>> i = jm.Element("i", n)
32
+ >>> x = jm.Binary("x", shape=(n, n))
33
+ >>> x_i = x[i] # [i] は xi を生成するたびにuuidが変わるので replaceで使うには一度だけ生成したオブジェクトを使い回す必要がある
34
+ >>> expr = jm.Sum(i, x_i)
35
+ >>> u = jm.Integer("u", shape=(n,), lower=0, upper=10)
36
+ >>> k = jm.Placeholder("k")
37
+ >>> j = jm.Element("j", k)
38
+ >>> y = jm.Placeholder("y", shape=(n, k))
39
+ >>> replaced = replace(expr, x_i, jm.Sum(j, u[i]*y[i,j]), check_id=True)
40
+ >>> print(replaced)
41
+ >>> Sum_{i=0}^{n}(Sum_{j=0}^{k}(u[i]*y[i,j]))
42
+
43
+ >>> import jijmodeling as jm
44
+ >>> n = jm.Placeholder("n")
45
+ >>> i = jm.Element("i", n)
46
+ >>> x = jm.Binary("x", shape=(n, n))
47
+ >>> expr = jm.Sum(i, x[i])
48
+ >>> u = jm.Integer("u", shape=(n,), lower=0, upper=10)
49
+ >>> k = jm.Placeholder("k")
50
+ >>> j = jm.Element("j", k)
51
+ >>> y = jm.Placeholder("y", shape=(n, k))
52
+ >>> replaced = replace(expr, x[i], jm.Sum(j, u[i]*y[i,j]), check_id=False) # check_id=False にすると、uuidが異なっていても置換できる
53
+ >>> print(replaced)
54
+ >>> Sum_{i=0}^{n}(Sum_{j=0}^{k}(u[i]*y[i,j]))
55
+ """
56
+ if expr_same(expr, target, check_id):
57
+ return source
58
+
59
+ return _replace(expr, target, source, check_id)
60
+
61
+
62
+ @singledispatch
63
+ def _replace(
64
+ expr: jm.Expression, target: jm.Expression, source: jm.Expression, check_id: bool
65
+ ) -> jm.Expression:
66
+ raise TypeError(f"{type(expr)} is not supported in `replace`.")
67
+
68
+
69
+ # List all the types that are inherited from `jm.Expression` and register them to `_replace` function.
70
+ @_replace.register
71
+ def _(expr: jm.ArrayShape, target: jm.Expression, source: jm.Expression, check_id: bool) -> jm.Expression:
72
+ if expr_same(expr, target, check_id):
73
+ return source
74
+ if isinstance(source, (jm.Variable, jm.Subscripts)) and expr_same(expr.array, target, check_id):
75
+ return source.shape[expr.dim]
76
+ return expr
77
+
78
+
79
+ @_replace.register
80
+ def _(
81
+ expr: jm.expression.Number,
82
+ target: jm.Expression,
83
+ source: jm.Expression,
84
+ check_id: bool,
85
+ ) -> jm.Expression:
86
+ if expr_same(expr, target, check_id):
87
+ return source
88
+ return expr
89
+
90
+
91
+ @_replace.register
92
+ def _(
93
+ expr: jm.expression.BinaryOperator,
94
+ target: jm.Expression,
95
+ source: jm.Expression,
96
+ check_id: bool,
97
+ ) -> jm.Expression:
98
+ if expr_same(expr, target, check_id):
99
+ return source
100
+
101
+ cls = type(expr)
102
+ replaced_left = _replace(expr.left, target, source, check_id)
103
+ replaced_right = _replace(expr.right, target, source, check_id)
104
+ return cls(replaced_left, replaced_right)
105
+
106
+
107
+ @_replace.register
108
+ def _(
109
+ expr: jm.expression.UnaryOperator,
110
+ target: jm.Expression,
111
+ source: jm.Expression,
112
+ check_id: bool,
113
+ ) -> jm.Expression:
114
+ if expr_same(expr, target, check_id):
115
+ return source
116
+
117
+ cls = type(expr)
118
+ replaced = _replace(expr, target, source, check_id)
119
+ return cls(replaced)
120
+
121
+
122
+ @_replace.register
123
+ def _(
124
+ expr: jm.expression.sum.ReductionOperator,
125
+ target: jm.Expression,
126
+ source: jm.Expression,
127
+ check_id: bool,
128
+ ) -> jm.Expression:
129
+ if expr_same(expr, target, check_id):
130
+ return source
131
+
132
+ replaced_sum_index = tp.cast(
133
+ jm.Element, _replace(expr.sum_index, target, source, check_id)
134
+ )
135
+ replaced_operand = _replace(expr.operand, target, source, check_id)
136
+ replaced_condition = _replace_condition(expr.condition, target, source, check_id)
137
+ cls = type(expr)
138
+ return cls(replaced_sum_index, replaced_operand, replaced_condition)
139
+
140
+
141
+ @_replace.register
142
+ def _(
143
+ expr: jm.Variable, target: jm.Expression, source: jm.Expression, check_id: bool
144
+ ) -> jm.Expression:
145
+ if expr_same(expr, target, check_id):
146
+ return source
147
+ return expr
148
+
149
+
150
+
151
+ @_replace.register
152
+ def _(
153
+ expr: jm.Subscripts, target: jm.Expression, source: jm.Expression, check_id: bool
154
+ ) -> jm.Expression:
155
+ if expr_same(expr, target, check_id):
156
+ return source
157
+ replaced_variable = tp.cast(
158
+ jm.Variable, _replace(expr.variable, target, source, check_id)
159
+ )
160
+ replaced_subscripts = [
161
+ _replace(s, target, source, check_id) for s in expr.subscripts
162
+ ]
163
+ cls = type(expr)
164
+ return cls(replaced_variable, replaced_subscripts)
165
+
166
+
167
+ @singledispatch
168
+ def _replace_condition(
169
+ condition: jc.Condition,
170
+ target: jm.Expression,
171
+ source: jm.Expression,
172
+ check_id: bool,
173
+ ) -> jc.Condition:
174
+ raise TypeError(f"{type(condition)} is not supported in `replace`.")
175
+
176
+
177
+ @_replace_condition.register
178
+ def _(
179
+ condition: jc.NoneCondition,
180
+ target: jm.Expression,
181
+ source: jm.Expression,
182
+ check_id: bool,
183
+ ) -> jc.Condition:
184
+ return condition
185
+
186
+
187
+ @_replace_condition.register
188
+ def _(
189
+ condition: jc.CompareCondition,
190
+ target: jm.Expression,
191
+ source: jm.Expression,
192
+ check_id: bool,
193
+ ) -> jc.Condition:
194
+ replaced_left = _replace(condition.left, target, source, check_id)
195
+ replaced_right = _replace(condition.right, target, source, check_id)
196
+ cls = type(condition)
197
+ return cls(replaced_left, replaced_right)
198
+
199
+
200
+ @_replace_condition.register
201
+ def _(
202
+ condition: jc.ConditionOperator,
203
+ target: jm.Expression,
204
+ source: jm.Expression,
205
+ check_id: bool,
206
+ ) -> jc.Condition:
207
+ replaced_left = _replace_condition(condition.left, target, source, check_id)
208
+ replaced_right = _replace_condition(condition.right, target, source, check_id)
209
+ cls = type(condition)
210
+ return cls(replaced_left, replaced_right)