# Matrix Multiplying Problem Generator 3 # Author: Aidan Murphy # Last Modified: 3/9/17 # This code will generate a TeX file with a variable number of matrix multiplication practice problems # These are the variables which you may change the values of for different results: numberExamples = 20 # the number of examples that this program will generate minSizeDimension = 2 # the minimum dimension of each matrix (must be >0) maxSizeDimension = 5 # the maximum dimension of each matrix (must be >0) probOfResize = 0.8 # the minimum proportion of products that will exist minNum = -5 # the minimum cell entry in the matrices maxNum = 5 # the maximum cell entry in the matrices # Abandon all hope, ye who enter here ##################################################################################################### # Here there be dragons import numpy as np # import package for matrices # opens the text document to edit text_file = open("M233-MMG-Exercises.tex", "w") # starting the inserting of the prelude preludeList = ["\documentclass[12pt]{article}", "\ usepackage[english]{babel}", "\ usepackage[utf8]{inputenc}", "\ usepackage[autostyle, english = american]{csquotes}", "\ usepackage{listings}", "\ usepackage{keyval}", "\ usepackage{xcolor}", "\ usepackage{textcomp}", "\ usepackage{fullpage}", "\ usepackage{setspace}", "\ usepackage{graphicx}", "\ usepackage{hyperref}", "\ usepackage{verbatim}", "\ usepackage{multicol}", "\ usepackage{float}", "\ usepackage{framed}", "\ usepackage[fleqn]{amsmath}", "\ usepackage{amssymb}", "\ usepackage{amsfonts}", "\ usepackage{amsthm}", "\ usepackage{marvosym}", "\ usepackage{wasysym}", "\ usepackage{tikz}", "\ usetikzlibrary{arrows}", "\ newtheorem{theorem}{Theorem}", "\ newtheorem{lemma}{Lemma}", "\ renewcommand\qedsymbol{$\blacksquare$}"] # vector containing the prelude commands and packages listLength = len(preludeList) for i in range(0, listLength): text_file.write(preludeList[i].replace(" ", "")) # get rid of strings # declare the beginning of the document prelim = "\ begin{document}" text_file.write(prelim.replace(" ", "")) # Build the title of the document prelim = "\ noindent" text_file.write(prelim.replace(" ", "")) text_file.write(" Math 233 Matrix Multiplication Example Generator") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" Author: Aidan Murphy") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" Last Modified: 3/9/17") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # input the description prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write( " This document was generated as a .txt file by Python, and then compiled with TeXMaker. The purpose of this document is to generate pairs of matrices, as well as their products if they exist, for the purpose of practicing matrix multiplication.") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write(" Practice Problems: ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # generating matrix examples firstMatrixList = [None for x in range(numberExamples)] secondMatrixList = [None for y in range(numberExamples)] answers = [None for z in range(numberExamples)] for i in range(0, numberExamples): # generating the matrices with Python m1 = np.random.randint(low=minSizeDimension, high=maxSizeDimension) n1 = np.random.randint(low=minSizeDimension, high=maxSizeDimension) m2 = np.random.randint(low=minSizeDimension, high=maxSizeDimension) n2 = np.random.randint(low=minSizeDimension, high=maxSizeDimension) if np.random.rand(1) < probOfResize: n1 = m2 # so that a higher proportion of products exist M1 = np.random.randint(low=minNum, high=maxNum, size=(m1, n1)) M2 = np.random.randint(low=minNum, high=maxNum, size=(m2, n2)) if n1 == m2: answers[i] = np.matmul(M1, M2) else: answers[i] = "Product does not exist." # start printing these results to the TeX file prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write(" Problem %i " % (i + 1)) prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # trying to extend to general sized matrices prelim = "$\ begin{bmatrix}" text_file.write(prelim.replace(" ", "")) for j in range(m1): for k in range(n1): text_file.write("{}".format(M1.item((j, k)))) if k < n1: text_file.write(" & ") prelim = "\ \ " text_file.write(prelim.replace(" ", "")) prelim = "\ end{bmatrix}$" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # generating the second matrix prelim = "$\ begin{bmatrix}" text_file.write(prelim.replace(" ", "")) for j in range(m2): for k in range(n2): text_file.write("{}".format(M2.item((j, k)))) if k < n2: text_file.write(" & ") prelim = "\ \ " text_file.write(prelim.replace(" ", "")) prelim = "\ end{bmatrix}$" text_file.write(" ") text_file.write(prelim.replace(" ", "")) # indent to a new line prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # skipping a page before printing the solutions prelim = "\ newpage" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ hspace{0 mm}" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newpage" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # begin the page with the solutions prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write(" Solutions: ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") for i in range(0, numberExamples): # start printing these results to the TeX file prelim = "\ indent" text_file.write(prelim.replace(" ", "")) text_file.write(" Problem %i " % (i + 1)) prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # print out answer matrices thisAnswer = answers[i] if isinstance(thisAnswer, str) == 0: m = len(thisAnswer[:, 1]) n = len(thisAnswer[1, :]) prelim = "$\ begin{bmatrix}" text_file.write(prelim.replace(" ", "")) for j in range(m): for k in range(n): text_file.write("{}".format(answers[i].item((j, k)))) if k < n: text_file.write(" & ") prelim = "\ \ " text_file.write(prelim.replace(" ", "")) prelim = "\ end{bmatrix}$" text_file.write(prelim.replace(" ", "")) text_file.write(" ") else: text_file.write("{}".format(thisAnswer)) # indent to a new line prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") prelim = "\ newline" text_file.write(prelim.replace(" ", "")) text_file.write(" ") # declare the end of the document prelim = "\ end{document}" text_file.write(prelim.replace(" ", "")) text_file.close()