def _parseGroup(tup): def Error(index, message): return Left("system #" + str(index) + ": " + message) (lines, index) = tup one = lines[0].split() two = lines[1].split() line3 = lines[2].strip() line4 = lines[3].strip() if len(one) == 0 or len(two) == 0: return Error(index, "lines 1 or 2 empty") if not forall(one + two, isFraction): return Error(index, "at least one token on line #1 or #2 is not a valid fractional number") n = len(one) np = len(two) if np % n != 0: return Error(index, "number of tokens on line #2 is not multiple of number of tokens on #1") p = np / n if line3 != noSolutionSymbol and line4 != noSolutionSymbol: three = lines[2].split() if not forall(three, isFraction): return Error(index, "at least one token on line #3 is not a valid fractional number") if len(three) != p: return Error(index, "line #3 is the wrong size for being a particular solution") pSol = map(fractionMaker, three) if line4 == nullVecSpaceSymbol: kerBase = [] else: four = line4.split() if not forall(four, isFraction): return Error(index, "at least one token on line #4 is not a valid fractional number") pq = len(four) if pq % p != 0: return Error(index, "number of tokens on line #4 is not multiple of number of token on #3") kerBase = grouped(map(fractionMaker, four), p) maybeSolution = Just((kerBase, pSol)) elif line3 == noSolutionSymbol and line4 == noSolutionSymbol: maybeSolution = Nothing else: return Error(index, "lines #3 and #4 are invalid, somehow") rightSide = map(fractionMaker, one) leftSide = matrixFromList(map(fractionMaker, two), n, p) system = (leftSide, rightSide) out = (system, maybeSolution) return Right(out)
def isValidLine(line): if len(line) <= 1: error(isValidLine, "input list is too short to be part of a `System n`") leftSide = line[:-1] rightSide = line[-1] if forall(leftSide, isZero): return isZero(rightSide) else: return True
def _parseGroup(tup): def Error(index, message): return Left("system #" + str(index) + ": " + message) (group, index) = tup one = group[0].split() two = group[1].split() three = group[2].split() four = group[3].split() if len(one) != 1 or len(two) != 1 or len(three) == 0 or len(four) == 0: return Error(index, "lines 1 or 2 too long, or lines 3 or 4 empty") if not isNat(one[0]) or not isNat(two[0]): return Error(index, "line 1 or 2 does not contain a natural integer") lineNumber = int(one[0]) varNumber = int(two[0]) if lineNumber == 0 or varNumber == 0: return Error( index, "line 1 or 2 does not contain a non-zero natural integer") if len(three) != lineNumber * varNumber or len(four) != lineNumber: return Error( index, "length of line 3 or 4 don't match values of lines 1 and 2") if not forall(three + four, isFraction): return Error( index, "at least one token on line 3 or 4 is not a valid fraction") leftSideMatrix = matrixFromList(map(fractionMaker, three), lineNumber, varNumber) rightSideVector = map(fractionMaker, four) out = (leftSideMatrix, rightSideVector) return Right(out)
def keepPivotalLines(system): if not forall(system.nonPivotalLines, isNullVector): error( keepPivotalLines, "somehow after successfully echelonizing, " + "one non pivotal line is not full of zeroes") return system.pivotalLines
def maybeSystem(pivotalLines, nonPivotalLines): if forall(pivotalLines + nonPivotalLines, isValidLine): return Maybe(value=System(pivotalLines, nonPivotalLines)) else: return Nothing