From c2921119b2ee6d05d4b635cf11a8d1a8778d7b01 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 15 Dec 2024 07:15:13 -0500 Subject: [PATCH] Start v2.0.0 test --- .gitignore | 1 + test/test_combinatorica_v09.py | 7 +- test/test_combinatorica_v20.py | 433 +++++++++++++++++++++++++++++++++ 3 files changed, 438 insertions(+), 3 deletions(-) create mode 100644 test/test_combinatorica_v20.py diff --git a/.gitignore b/.gitignore index 1f49f66..ead23ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ +__pycache__ /.python-version /Mathics3_Combinatorica.egg-info /tmp diff --git a/test/test_combinatorica_v09.py b/test/test_combinatorica_v09.py index 7b38f0b..e1e893c 100644 --- a/test/test_combinatorica_v09.py +++ b/test/test_combinatorica_v09.py @@ -31,10 +31,11 @@ def reset_and_load_package(): # A number of examples from: -# * Implementing Discrete Mathematics by Steven Skiena and -# * Computation Discrete Mathematics by Sriram Pemmaraju and Steven Skiena. +# Implementing Discrete Mathematics: Combinatorics and Graph Theory with Mathematica +# by Steven S. Skiena, Addison-Wesley Publishing Co., Advanced Book Program, +# 350 Bridge Parkway, Redwood City CA 94065. ISBN 0-201-50943-1. -# Page numbers below come from the first book +# Page numbers below come from thise. book # Some tests have been altered to speed them up, or to make the intent # more clear in a test. diff --git a/test/test_combinatorica_v20.py b/test/test_combinatorica_v20.py new file mode 100644 index 0000000..8b0f258 --- /dev/null +++ b/test/test_combinatorica_v20.py @@ -0,0 +1,433 @@ +# -*- coding: utf-8 -*- + +import os.path as osp +from test.helper import check_evaluation, evaluate, reset_session + +import pytest + +# This variable is set to initialize the module just once, +# and just before running the tests. +_initialized: bool = False + +combinatorica_file = osp.normpath( + osp.join(osp.dirname(__file__), + "..", "Combinatorica", + "CombinatoricaV2.0.0.m") +) + + +@pytest.fixture(autouse=True) +def reset_and_load_package(): + global _initialized + if not _initialized: + reset_session() + evaluate( + f""" + Get["{combinatorica_file}"] + """ + ) + _initialized = True + yield + + +# A number of examples from: +# * Implementing Discrete Mathematics by Steven Skiena and +# * Computation Discrete Mathematics by Sriram Pemmaraju and Steven Skiena. + +# Page numbers below come from this book. +# Some tests have been altered to speed them up, or to make the intent +# more clear in a test. + + +def test_permutations_and_subsets_1_1_1(): + for str_expr, str_expected, message in ( + ( + "Permutations[3]", + "{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, " + "{3, 1, 2}, {3, 2, 1}}", + "Permutations; 1.1.1 Page 3", + ), + ( + "Permute[{A, B, C, D}, Permutations[3]]", + "{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, " + "{C, B, A}}", + "Permute operator; 1.1.1 Page 3", + ), + ( + "Permute[{5, 2, 4, 3, 1}, InversePermutation[{5, 2, 4, 3, 1}]]", + "{1, 2, 3, 4, 5}", + "Inverse permutation, 1.1.1 Page 3", + ), + ( + "MinimumChangePermutations[{a, b, c}]", + "{{a, b, c}, {b, a, c}, {c, a, b}, {a, c, b}, " + "{b, c, a}, {c, b, a}}", + "Minimum-change permutation, 1.1.1, Page 3", + ), + ( + "RankPermutation[{8, 9, 7, 1, 6, 4, 5, 3, 2}]", + "321953", + "ranking permutation function; 1.1.1, Page 3", + ), + ( + "UnrankPermutation[321953, 9]", + "{8, 9, 7, 1, 6, 4, 5, 3, 2}", + "Unrank permutation, 1.1.1, Page 4", + ), + ( + "UnrankPermutation[0, 9]", + "{1, 2, 3, 4, 5, 6, 7, 8, 9}", + "additional test 1 - not part of text]", + ), + ( + "UnrankPermutation[1, 9]", + "{1, 2, 3, 4, 5, 6, 7, 9, 8}", + "additional test 2 - not part of text]", + ), + ( + "UnrankPermutation[9!-1, 9]", + "{9, 8, 7, 6, 5, 4, 3, 2, 1}", + "additional test 3 - not part of text]", + ), + ( + "p=RandomPermutation[50]; " + "Inversions[p] == Inversions[InversePermutation[p]]", + "True", + "inversions, 1.1.1, Page 4", + ), + # ( + # "NecklacePolynomial[6, {a, b, c}, Cyclic]" + # "???", + # "Necklace polynomial, 1.1.1 Page 4", + # ), + # ( + # "Subsets[{1,2,3,4}]" + # "??", + # "MinimumChangePermuations; 1.1.1, Page 5", + # ), + # ( + # "KSubsets[{1,2,3,4,5}, 3]", + # "??", + # "k subsets, 1.1.1, Page 5", + # ), + ): + check_evaluation(str_expr, str_expected, message) + + +def test_partitions_compositions_and_young_tableaux_1_1_2(): + for str_expr, str_expected, message in ( + # ( + # "Partitions[6]", + # "???" + # "partitions, 1.1.2 Page 7", + # ), + ( + "Compositions[5, 3]", + "{{0, 0, 5}, {0, 1, 4}, {0, 2, 3}, {0, 3, 2}, " + "{0, 4, 1}, {0, 5, 0}, {1, 0, 4}, {1, 1, 3}, {1, 2, 2}, " + "{1, 3, 1}, {1, 4, 0}, {2, 0, 3}, {2, 1, 2}, {2, 2, 1}, " + "{2, 3, 0}, {3, 0, 2}, {3, 1, 1}, {3, 2, 0}, {4, 0, 1}, " + "{4, 1, 0}, {5, 0, 0}}", + "Compositions, 1.1.2 Page 7", + ), + ( + "SetPartitions[3]", + "{{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 2}, {3}}, " + "{{1, 3}, {2}}, {{1}, {2}, {3}}}", + "Set partitions, 1.1.2 Page 7", + ), + # ( + # "Tableaux[{2,2,1}", + # "???", + # "Tableaux, 1.1.2 Page 8", + # ), + ): + check_evaluation(str_expr, str_expected, message, to_string_expr=True) + + +# def test_inversions_and_inversion_vectors_1_3(): +# for str_expr, str_expected, message in ( +# ( +# "p = {5,9,1,8,2,6,4,7,3}; ToInversionVector[p]", +# "{2, 3, 6, 4, 0, 2, 2, 1}", +# "ToInversionVector 1.3.1, Page 27", +# ), +# ( +# "FromInversionVector[ToInversionVector[p]]", +# "p", +# "FromInversionVector 1.3.1, Page 28", +# ), +# ( +# "h = InversePermutation[p]; " +# "g = MakeGraph[Range[Length[p]], ((#1<#2 && h[[#1]]>h[[#2]]) || (#1>#2 && h[[#1]]