Skip to content

Commit 0fdc9ab

Browse files
committed
first comprehension syntax desugaring
1 parent e2fd735 commit 0fdc9ab

4 files changed

Lines changed: 106 additions & 16 deletions

File tree

docs/en/releases/unreleased.ipynb

Lines changed: 34 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/ja/releases/unreleased.ipynb

Lines changed: 32 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

markdowns/en/releases/unreleased.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,34 @@ This is similar to {py:func}`~numpy.fromfunction` in NumPy.
2828
import jijmodeling as jm
2929
3030
31+
problem = jm.Problem("genarray example")
32+
N = problem.Natural("N")
33+
M = problem.Natural("M")
34+
a = problem.Float("a", shape=(N, M))
35+
x = problem.BinaryVar("x", shape=N)
36+
Sums = problem.NamedExpr("Sums", jm.genarray(lambda i, j: a[i, j] * x[i], (N, M)))
37+
38+
39+
problem
40+
```
41+
42+
When using the Decorator API, you can also use a comprehension syntax with `jm.genarray` as follows:
43+
44+
```{code-cell} ipython3
3145
@jm.Problem.define("genarray example")
3246
def problem(problem):
3347
N = problem.Natural()
3448
M = problem.Natural()
3549
a = problem.Float(shape=(N, M))
3650
x = problem.BinaryVar(shape=N)
37-
Sums = problem.NamedExpr(jm.genarray(lambda i, j: a[i, j] * x[i], (N, M)))
51+
Sums = problem.NamedExpr(jm.genarray(a[i, j] * x[i] for i, j in (N, M)))
52+
```
3853

54+
Comprehensions used with `jm.genarray` MUST satisfy the following conditions:
3955

40-
problem
41-
```
56+
1. It must be a **generator expression** enclosed in parentheses (or omitted). List comprehensions enclosed in `[ ]` are not supported.
57+
2. The `for` clause must be a single loop of the form `for i_1, .., i_k in (N_1, .., N_k)` for natural-number expressions `N_i`.
58+
If the arity and types match, the variable names may be arbitrary, and the expression itself may be complex.
4259

4360
### Support for `min` / `max` along axes
4461

markdowns/ja/releases/unreleased.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,34 @@ kernelspec:
2828
import jijmodeling as jm
2929
3030
31+
problem = jm.Problem("genarray example")
32+
N = problem.Natural("N")
33+
M = problem.Natural("M")
34+
a = problem.Float("a", shape=(N, M))
35+
x = problem.BinaryVar("x", shape=N)
36+
Sums = problem.NamedExpr("Sums", jm.genarray(lambda i, j: a[i, j] * x[i], (N, M)))
37+
38+
39+
problem
40+
```
41+
42+
また、Decorator API を利用している場合、以下のように `jm.genarray` で内包表記を用いることもできます:
43+
44+
```{code-cell} ipython3
3145
@jm.Problem.define("genarray example")
3246
def problem(problem):
3347
N = problem.Natural()
3448
M = problem.Natural()
3549
a = problem.Float(shape=(N, M))
3650
x = problem.BinaryVar(shape=N)
37-
Sums = problem.NamedExpr(jm.genarray(lambda i, j: a[i, j] * x[i], (N, M)))
51+
Sums = problem.NamedExpr(jm.genarray(a[i, j] * x[i] for i, j in (N, M)))
52+
```
3853

54+
`jm.genarray` で使える内包表記は以下の条件に従う必要があります:
3955

40-
problem
41-
```
56+
1. 丸括弧で囲まれた**ジェネレータ式**であること。 `[ ]` で囲まれたリスト内包表記は利用できません。
57+
2. `for` 部は自然数式 `N_i` に対し、 `for i_1, .., i_k in (N_1, .., N_k)` の形の単一のループであること。
58+
+ 個数と型が合っていれば、変数名は何でも構いませんし、複雑な式を書くこともできます。
4259

4360
## 軸に沿った `min` / `max` のサポート
4461

0 commit comments

Comments
 (0)