Skip to content

Commit

Permalink
Merge 8def056 into 1dd9911
Browse files Browse the repository at this point in the history
  • Loading branch information
notimeforcaution committed Jul 8, 2019
2 parents 1dd9911 + 8def056 commit 308165b
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 16 deletions.
78 changes: 78 additions & 0 deletions visma/discreteMaths/boolean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from visma.simplify.simplify import simplify
from visma.functions.constant import Constant
from visma.io.tokenize import tokenizer


def logicalAND(token1, token2):
# TODO: Comments, animations & test cases.
comments = []
animations = []
token1, _, _, _, _ = simplify(token1)
token2, _, _, _, _ = simplify(token2)
if isinstance(token1, Constant) and isinstance(token2, Constant):
comments += [['Converting numbers to Binary Illustrations: ']]
animations += [[]]
binaryValue1 = token1.binary()
binaryValue2 = token2.binary()
comments += [[]]
animations += [[tokenizer('a = ' + str(binaryValue1))]]
comments += [[]]
animations += [[tokenizer('b = ' + str(binaryValue2))]]
comments += [['Doing AND operation for each of the consecutive bit']]
animations += [[]]
resultValue = token1.calculate() & token2.calculate()
comments += [['Final result is']]
animations += [[tokenizer('r = ' + str(resultValue))]]
token_string = 'r = ' + str(resultValue)
return token_string, animations, comments
else:
return '', [], []


def logicalOR(token1, token2):
# TODO: Comments, animations & test cases.
comments = []
animations = []
token1, _, _, _, _ = simplify(token1)
token2, _, _, _, _ = simplify(token2)
if isinstance(token1, Constant) and isinstance(token2, Constant):
comments += [['Converting numbers to Binary Illustrations: ']]
animations += [[]]
binaryValue1 = token1.binary()
binaryValue2 = token2.binary()
comments += [[]]
animations += [[tokenizer('a = ' + str(binaryValue1))]]
comments += [[]]
animations += [[tokenizer('b = ' + str(binaryValue2))]]
comments += [['Doing OR operation for each of the consecutive bit']]
animations += [[]]
resultValue = token1.calculate() | token2.calculate()
comments += [['Final result is']]
animations += [[tokenizer('r = ' + str(resultValue))]]
token_string = 'r = ' + str(resultValue)
return token_string, animations, comments
else:
return '', [], []


def logicalNOT(token1):
# TODO: Test cases.
comments = []
animations = []
token1, _, _, _, _ = simplify(token1)
if isinstance(token1, Constant):
comments += [['Converting numbers to Binary Illustrations: ']]
animations += [[]]
binaryValue1 = token1.binary()
comments += [[]]
animations += [[tokenizer('a = ' + str(binaryValue1))]]
resultValueBinary = bin((1 << 8) - 1 - int(binaryValue1, 2))
resultValue = int(resultValueBinary, 2)
comments += [['Final binary is']]
animations += [[tokenizer('r = ' + str(resultValueBinary))]]
comments += [['Final result is']]
animations += [[tokenizer('r = ' + str(resultValue))]]
token_string = 'r = ' + str(resultValue)
return token_string, animations, comments
else:
return '', [], []
49 changes: 44 additions & 5 deletions visma/discreteMaths/combinatorics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ def factorial(tokens):
value = int(tokens[0].calculate())
if value == 0:
result = [Constant(1)]
comments += [['Factorial of ZERO is defined to be 1']]
animation += [tokenizer('f = ' + str(1))]
else:
resultString = ''
for i in range(1, value + 1):
resultString += (str(i) + '*')
resultString = resultString[:-1]
resultTokens = tokenizer(resultString)
comments += [['Expanding the factorial as']]
animation += [resultTokens]
result, _, _, _, _ = simplify(resultTokens)
token_string = tokensToString(result)
comments += [['Hence result: ']]
animation += [tokenizer('f = ' + token_string)]
return result, [], token_string, animation, comments


Expand All @@ -59,10 +65,24 @@ def permutation(nTokens, rTokens):
animation = []
comments = []
if (isinstance(nTokens[0], Constant) & len(nTokens) == 1) & (isinstance(rTokens[0], Constant) & len(rTokens) == 1):
numerator, _, _, _, _ = factorial(nTokens)
comments += [['nCr is defined as (n!)/(r!)*(n-r)!']]
animation += [[]]
comments += [['Solving for n!']]
animation += [[]]
numerator, _, _, animNew1, commentNew1 = factorial(nTokens)
commentNew1[1] = ['(n)! is thus solved as: ']
animation.extend(animNew1)
comments.extend(commentNew1)
denominator = nTokens[0] - rTokens[0]
denominator, _, _, _, _ = factorial([denominator])
comments += [['Solving for (n - r)!']]
animation += [[]]
denominator, _, _, animNew2, commentNew2 = factorial([denominator])
commentNew2[1] = ['(n - r)! is thus solved as: ']
comments.extend(commentNew2)
animation.extend(animNew2)
result = [numerator[0] / denominator[0]]
comments += [['On placing values in (n!)/(n-r)!']]
animation += [tokenizer('r = ' + tokensToString(result))]
token_string = tokensToString(result)
return result, [], token_string, animation, comments

Expand All @@ -86,11 +106,30 @@ def combination(nTokens, rTokens):
animation = []
comments = []
if (isinstance(nTokens[0], Constant) & len(nTokens) == 1) & (isinstance(rTokens[0], Constant) & len(rTokens) == 1):
numerator, _, _, _, _ = factorial(nTokens)
comments += [['nCr is defined as (n!)/(r!)*(n-r)!']]
animation += [[]]
comments += [['Solving for n!']]
animation += [[]]
numerator, _, _, animNew1, commentNew1 = factorial(nTokens)
commentNew1[1] = ['(n)! is thus solved as: ']
animation.extend(animNew1)
comments.extend(commentNew1)
denominator1 = nTokens[0] - rTokens[0]
denominator1, _, _, _, _ = factorial([denominator1])
denominator2, _, _, _, _ = factorial([rTokens[0]])
comments += [['Solving for (n - r)!']]
animation += [[]]
denominator1, _, _, animNew2, commentNew2 = factorial([denominator1])
commentNew2[1] = ['(n - r)! is thus solved as: ']
comments.extend(commentNew2)
animation.extend(animNew2)
comments += [['Solving for r!']]
animation += [[]]
denominator2, _, _, animNew3, commentNew3 = factorial([rTokens[0]])
commentNew3[1] = ['r! is thus solved as: ']
comments.extend(commentNew3)
animation.extend(animNew3)
denominator = denominator1[0] * denominator2[0]
result = [numerator[0] / denominator]
comments += [['On placing values in (n!)/(r!)*(n-r)!']]
animation += [tokenizer('r = ' + tokensToString(result))]
token_string = tokensToString(result)
return result, [], token_string, animation, comments
23 changes: 23 additions & 0 deletions visma/discreteMaths/probability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from visma.io.tokenize import tokenizer


def simpleProbability(sampleSpace, requiredEvent=None):
animations = []
comments = []
events = []
token_string = ''
if sampleSpace.values is not []:
events.extend(sampleSpace.values)
totalOccurances = len(events)
animations += [[]]
comments += [['The total occurances are ' + str(totalOccurances)]]
requiredOccurances = events.count(requiredEvent)
animations += [[]]
comments += [['The occurances of required event are ' + str(requiredOccurances)]]
probability = requiredOccurances/totalOccurances
comments += [['Hence, Required probability is: ']]
animations += [tokenizer('P = ' + str(probability))]
token_string = 'P = ' + str(probability)
return token_string, animations, comments
else:
return '', [], []
75 changes: 75 additions & 0 deletions visma/discreteMaths/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from collections import Counter
from visma.io.tokenize import tokenizer
from visma.simplify.simplify import simplify
from visma.io.parser import tokensToString
from visma.functions.constant import Constant


class sampleSpace(object):
values = []
size = 0

def __init__(self, values):
if values is not None:
self.values = values
self.size = len(values)


def ArithemeticMean(sampleSpace):
animations = []
comments = []
if sampleSpace.values is not []:
sum = sum(sampleSpace.values)
animations += [[]]
comments += [['Sum of all the values of the sample space provided by user: ' + sum]]
summationString = ''
for val in sampleSpace.values:
summationString += str(val) + '+'
summationString = summationString[:-1]
summationTokens = tokenizer(summationString)
resultTokens, _, _, _, _ = simplify(summationTokens)
if len(resultTokens) == 1 and isinstance(resultTokens, Constant):
ArithemeticMean = resultTokens/Constant(len(sampleSpace.values))
animations += [[]]
comments += [['Considering ' + len(sampleSpace.values) + ' values.']]
animations += [[tokenizer('mean = ' + str(ArithemeticMean.calculate))]]
token_string = tokensToString(ArithemeticMean)
return token_string, animations, comments
else:
return '', [], []


def Mode(sampleSpace):
animations = []
comments = []
token_string = ''
if sampleSpace.values is not []:
mode, frequency = Counter(sampleSpace.values).most_common(1)[0]
comments += [['The mode refers to the most occuring element']]
animations += [[]]
comments += [['Mode = ' + str(mode) + '; Mode Frequence = ' + str(frequency)]]
animations += [[]]
token_string = 'Mode = ' + str(mode) + '; Mode Frequence = ' + str(frequency)
return token_string, animations, comments
else:
return '', [], []


def Median(sampleSpace):
animations = []
comments = []
token_string = ''
if sampleSpace.values is not []:
sizeSampleSpace = sampleSpace.size
if sizeSampleSpace % 2 == 1:
medianValue = sorted(sampleSpace.values)[sizeSampleSpace//2]
else:
medianValue = sum(sorted(sampleSpace.values)[sizeSampleSpace//(2-1): sizeSampleSpace//2+1])/2.0
comments += [['The median refers to the middle element in sorted sample space']]
animations += [[]]
comments += [['Median = ' + str(medianValue)]]
animations += [[]]
token_string = 'Median = ' + str(medianValue)
return token_string, animations, comments
else:
return '', [], []
5 changes: 5 additions & 0 deletions visma/functions/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def calculate(self):
def functionOf(self):
return []

def binary(self):
'''Returns a binary string of the given constant
'''
return bin(self.calculate())[2:]


class Zero(Constant):

Expand Down
13 changes: 12 additions & 1 deletion visma/gui/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from visma.calculus.differentiation import differentiate
from visma.calculus.integration import integrate
from visma.discreteMaths.combinatorics import factorial, combination, permutation
from visma.io.checks import checkTypes
from visma.io.tokenize import tokenizer, getLHSandRHS
from visma.io.parser import resultStringCLI
Expand All @@ -23,7 +24,7 @@ def commandExec(command):
inputEquation = inputEquation.split(',')[0]

simul = False
if ';' in inputEquation:
if (inputEquation.count(';') == 2) and (operation == 'solve'):
simul = True
afterSplit = inputEquation.split(';')
eqStr1 = afterSplit[0]
Expand Down Expand Up @@ -99,6 +100,16 @@ def commandExec(command):
else:
lhs, rhs = getLHSandRHS(tokens)
lTokens, rTokens, _, _, equationTokens, comments = solveFor(lTokens, rTokens, varName)
elif operation == 'factorial':
tokens, _, _, equationTokens, comments = factorial(tokens)
elif operation == 'combination':
n = tokenizer(inputEquation)
r = tokenizer(varName)
tokens, _, _, equationTokens, comments = combination(n, r)
elif operation == 'permutation':
n = tokenizer(inputEquation)
r = tokenizer(varName)
tokens, _, _, equationTokens, comments = permutation(n, r)
elif operation == 'integrate':
lhs, rhs = getLHSandRHS(tokens)
lTokens, _, _, equationTokens, comments = integrate(lTokens, varName)
Expand Down
44 changes: 35 additions & 9 deletions visma/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from visma.calculus.differentiation import differentiate
from visma.calculus.integration import integrate
from visma.discreteMaths.combinatorics import factorial, combination, permutation
from visma.io.checks import checkTypes, getVariables, getVariableSim, mathError
from visma.io.tokenize import tokenizer, getLHSandRHS
from visma.io.parser import resultLatex
Expand Down Expand Up @@ -365,16 +366,26 @@ def interactionMode(self):
if len(self.input) == 0:
return self.warning("No input given!")
self.simul = False
self.combi = False
if ';' in self.input:
self.simul = True
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = afterSplit[2]
if (self.input.count(';') == 2):
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = afterSplit[2]
elif (self.input.count(';') == 1):
self.combi = True
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = ''
if self.simul:
self.tokens = [tokenizer(eqStr1), tokenizer(eqStr2), tokenizer(eqStr3)]
self.addEquation()
operations = ['solve']
if self.combi:
operations.extend(['combination', 'permutation'])
self.solutionType = 'equation'
else:
self.tokens = tokenizer(self.input)
Expand Down Expand Up @@ -648,6 +659,16 @@ def calluser():
variables = getVariableSim(self.tokens)
self.wrtVariableButtons(variables, name)
self.resultOut = False
elif name == 'factorial':
self.tokens, availableOperations, tokenString, equationTokens, comments = factorial(self.tokens)
elif name == 'combination':
nTokens = self.tokens[0]
rTokens = self.tokens[1]
self.tokens, _, _, equationTokens, comments = combination(nTokens, rTokens)
elif name == 'permutation':
nTokens = self.tokens[0]
rTokens = self.tokens[1]
self.tokens, _, _, equationTokens, comments = permutation(nTokens, rTokens)
elif name == 'integrate':
lhs, rhs = getLHSandRHS(self.tokens)
variables = getVariables(lhs, rhs)
Expand Down Expand Up @@ -688,10 +709,16 @@ def calluser():
if varName == 'back':
if ';' in self.input:
self.simul = True
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = afterSplit[2]
if (self.input.count(';') == 2):
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = afterSplit[2]
elif (self.input.count(';') == 1):
afterSplit = self.input.split(';')
eqStr1 = afterSplit[0]
eqStr2 = afterSplit[1]
eqStr3 = ''
if self.simul:
self.tokens = [tokenizer(eqStr1), tokenizer(eqStr2), tokenizer(eqStr3)]
else:
Expand All @@ -705,7 +732,6 @@ def calluser():
self.refreshButtons(operations)

else:

if operation == 'solve':
if not self.simul:
self.lTokens, self.rTokens, availableOperations, tokenString, equationTokens, comments = solveFor(self.lTokens, self.rTokens, varName)
Expand Down
1 change: 1 addition & 0 deletions visma/io/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def checkTypes(lTokens=None, rTokens=None):
availableOperations.append('factorize')
availableOperations.append("integrate")
availableOperations.append("differentiate")
availableOperations.append("factorial")
inputType = "expression"

return availableOperations, inputType
Expand Down

0 comments on commit 308165b

Please sign in to comment.