QuizGenerator 0.4.2__py3-none-any.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.
- QuizGenerator/README.md +5 -0
- QuizGenerator/__init__.py +27 -0
- QuizGenerator/__main__.py +7 -0
- QuizGenerator/canvas/__init__.py +13 -0
- QuizGenerator/canvas/canvas_interface.py +627 -0
- QuizGenerator/canvas/classes.py +235 -0
- QuizGenerator/constants.py +149 -0
- QuizGenerator/contentast.py +1955 -0
- QuizGenerator/generate.py +253 -0
- QuizGenerator/logging.yaml +55 -0
- QuizGenerator/misc.py +579 -0
- QuizGenerator/mixins.py +548 -0
- QuizGenerator/performance.py +202 -0
- QuizGenerator/premade_questions/__init__.py +0 -0
- QuizGenerator/premade_questions/basic.py +103 -0
- QuizGenerator/premade_questions/cst334/__init__.py +1 -0
- QuizGenerator/premade_questions/cst334/languages.py +391 -0
- QuizGenerator/premade_questions/cst334/math_questions.py +297 -0
- QuizGenerator/premade_questions/cst334/memory_questions.py +1400 -0
- QuizGenerator/premade_questions/cst334/ostep13_vsfs.py +572 -0
- QuizGenerator/premade_questions/cst334/persistence_questions.py +451 -0
- QuizGenerator/premade_questions/cst334/process.py +648 -0
- QuizGenerator/premade_questions/cst463/__init__.py +0 -0
- QuizGenerator/premade_questions/cst463/gradient_descent/__init__.py +3 -0
- QuizGenerator/premade_questions/cst463/gradient_descent/gradient_calculation.py +369 -0
- QuizGenerator/premade_questions/cst463/gradient_descent/gradient_descent_questions.py +305 -0
- QuizGenerator/premade_questions/cst463/gradient_descent/loss_calculations.py +650 -0
- QuizGenerator/premade_questions/cst463/gradient_descent/misc.py +73 -0
- QuizGenerator/premade_questions/cst463/math_and_data/__init__.py +2 -0
- QuizGenerator/premade_questions/cst463/math_and_data/matrix_questions.py +631 -0
- QuizGenerator/premade_questions/cst463/math_and_data/vector_questions.py +534 -0
- QuizGenerator/premade_questions/cst463/models/__init__.py +0 -0
- QuizGenerator/premade_questions/cst463/models/attention.py +192 -0
- QuizGenerator/premade_questions/cst463/models/cnns.py +186 -0
- QuizGenerator/premade_questions/cst463/models/matrices.py +24 -0
- QuizGenerator/premade_questions/cst463/models/rnns.py +202 -0
- QuizGenerator/premade_questions/cst463/models/text.py +203 -0
- QuizGenerator/premade_questions/cst463/models/weight_counting.py +227 -0
- QuizGenerator/premade_questions/cst463/neural-network-basics/__init__.py +6 -0
- QuizGenerator/premade_questions/cst463/neural-network-basics/neural_network_questions.py +1314 -0
- QuizGenerator/premade_questions/cst463/tensorflow-intro/__init__.py +6 -0
- QuizGenerator/premade_questions/cst463/tensorflow-intro/tensorflow_questions.py +936 -0
- QuizGenerator/qrcode_generator.py +293 -0
- QuizGenerator/question.py +715 -0
- QuizGenerator/quiz.py +467 -0
- QuizGenerator/regenerate.py +472 -0
- QuizGenerator/typst_utils.py +113 -0
- quizgenerator-0.4.2.dist-info/METADATA +265 -0
- quizgenerator-0.4.2.dist-info/RECORD +52 -0
- quizgenerator-0.4.2.dist-info/WHEEL +4 -0
- quizgenerator-0.4.2.dist-info/entry_points.txt +3 -0
- quizgenerator-0.4.2.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import List, Tuple, Callable, Union, Any
|
|
3
|
+
import sympy as sp
|
|
4
|
+
|
|
5
|
+
from QuizGenerator.misc import Answer
|
|
6
|
+
|
|
7
|
+
def generate_function(rng, num_variables: int, max_degree: int, use_quadratic: bool = True) -> tuple[Any, sp.Expr, sp.MutableDenseMatrix, sp.Equality]:
|
|
8
|
+
"""
|
|
9
|
+
Generate a function, its gradient, LaTeX representation, and optimal point using SymPy.
|
|
10
|
+
Returns: (variables, function, gradient_function, equation)
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
rng: Random number generator
|
|
14
|
+
num_variables: Number of variables in the function
|
|
15
|
+
max_degree: Maximum degree of polynomial terms (only used if use_quadratic=False)
|
|
16
|
+
use_quadratic: If True, generates well-conditioned quadratic functions that converge nicely.
|
|
17
|
+
If False, uses the original random polynomial generation.
|
|
18
|
+
"""
|
|
19
|
+
# Create variable symbols
|
|
20
|
+
var_names = [f'x_{i}' for i in range(num_variables)]
|
|
21
|
+
variables = sp.symbols(var_names) # returns a tuple; robust when n==1
|
|
22
|
+
|
|
23
|
+
if use_quadratic:
|
|
24
|
+
# Generate well-conditioned quadratic function: f = sum of (x_i - center_i)^2 terms
|
|
25
|
+
# This creates a paraboloid with a clear minimum at (center_0, center_1, ...)
|
|
26
|
+
|
|
27
|
+
# Random center point (small integers for clean calculations)
|
|
28
|
+
centers = [rng.choice([-2, -1, 0, 1, 2]) for _ in range(num_variables)]
|
|
29
|
+
|
|
30
|
+
# Random positive coefficients for each squared term (keeps function convex)
|
|
31
|
+
# Use small values (0.5 to 2) to avoid huge gradients
|
|
32
|
+
coeffs = [rng.choice([0.5, 1, 1.5, 2]) for _ in range(num_variables)]
|
|
33
|
+
|
|
34
|
+
# Build quadratic: sum of coeff_i * (x_i - center_i)^2
|
|
35
|
+
poly = sp.Add(*[
|
|
36
|
+
coeffs[i] * (variables[i] - centers[i])**2
|
|
37
|
+
for i in range(num_variables)
|
|
38
|
+
])
|
|
39
|
+
|
|
40
|
+
else:
|
|
41
|
+
# Original random polynomial generation (may not converge well)
|
|
42
|
+
# monomials up to max_degree; drop constant 1
|
|
43
|
+
terms = [m for m in sp.polys.itermonomials(variables, max_degree) if m != 1]
|
|
44
|
+
|
|
45
|
+
# random nonzero integer coefficients in [-10,-1] ∪ [1,9]
|
|
46
|
+
coeff_pool = [*range(-10, 0), *range(1, 10)]
|
|
47
|
+
|
|
48
|
+
# polynomial; if no terms (e.g., max_degree==0), fall back to 0
|
|
49
|
+
poly = sp.Add(*(rng.choice(coeff_pool) * t for t in terms)) if terms else sp.Integer(0)
|
|
50
|
+
|
|
51
|
+
# f(x_1, ..., x_n) = poly
|
|
52
|
+
f = sp.Function('f')
|
|
53
|
+
function = poly
|
|
54
|
+
gradient_function = sp.Matrix([poly.diff(v) for v in variables])
|
|
55
|
+
equation = sp.Eq(f(*variables), poly)
|
|
56
|
+
|
|
57
|
+
return variables, function, gradient_function, equation
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def format_vector(vec: List[float]) -> str:
|
|
61
|
+
|
|
62
|
+
vector_string = ', '.join(
|
|
63
|
+
[
|
|
64
|
+
sorted(Answer.accepted_strings(v), key=lambda s: len(s))[0]
|
|
65
|
+
for v in vec
|
|
66
|
+
]
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if len(vec) == 1:
|
|
70
|
+
return vector_string
|
|
71
|
+
else:
|
|
72
|
+
return f"({vector_string})"
|
|
73
|
+
|