Ask Your Question

# Revision history [back]

There is no built-in way to construct such a set of matrices, but you can construct the set of integer lists of length n^2 with entries 0, 1, 2. Then you can subtract 1 from each entry to get entries -1, 0, 1, and then you can form a matrix from them:

sage: n = 3
sage: L = IntegerListsLex(length=n**2, max_part=2)
sage: len(L)
19683
sage: L[:6]
[[2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 0],
[2, 2, 2, 2, 2, 2, 2, 1, 2],
[2, 2, 2, 2, 2, 2, 2, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 1, 0]]
sage: [matrix(n, n, [b-1 for b in a]) for a in L[:6]]
[
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1], [1 1 0], [ 1  1 -1], [1 0 1], [1 0 0], [ 1  0 -1]
]

sage: M = [matrix(3, 3, [b-1 for b in a]) for a in L]
sage: len(M)
19683


There is no built-in way to construct such a set of matrices, but you can construct the set of integer lists of length n^2 with entries 0, 1, 2. Then you can subtract 1 from each entry to get entries -1, 0, 1, and then you can form a matrix from them:

sage: n = 3
sage: L = IntegerListsLex(length=n**2, max_part=2)
sage: len(L)
19683
sage: L[:6]
[[2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 0],
[2, 2, 2, 2, 2, 2, 2, 1, 2],
[2, 2, 2, 2, 2, 2, 2, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 1, 0]]
sage: [matrix(n, n, [b-1 for b in a]) for a in L[:6]]
[
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1]  [1 1 1]  [ 1  1  1]  [1 1 1]  [1 1 1]  [ 1  1  1]
[1 1 1], [1 1 0], [ 1  1 -1], [1 0 1], [1 0 0], [ 1  0 -1]
]

sage: M = [matrix(3, 3, [b-1 for b in a]) for a in L]
sage: len(M)
19683


Using M = (matrix(...)) with parentheses instead of square brackets is faster and more memory efficient, because it doesn't create the whole list at once. In particular, if you want to loop over the set, don't construct the whole list, but do something like this:

sage: for a in L:
....:    mat = matrix(3, 3, [b-1 for b in a])
....:    ... do stuff with mat ...
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://ask.sagemath.org/answers/63715/revisions/", "fetch_time": 1679709341000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-14/segments/1679296945292.83/warc/CC-MAIN-20230325002113-20230325032113-00258.warc.gz", "warc_record_offset": 151059789, "warc_record_length": 6060, "token_count": 1068, "char_count": 2075, "score": 3.640625, "int_score": 4, "crawl": "CC-MAIN-2023-14", "snapshot_type": "latest", "language": "en", "language_score": 0.6995329856872559, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00039-of-00064.parquet"}