Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gpkit as gp
- import re
- '''Фичалист:
- 1. Парсинг функций
- 2. Создание модели
- 3. Решение в удобоваримом виде
- 4. GUI
- 5. Сборка всего этого чуда в exe/другой файл (лишь бы поднималось)
- План работ:
- 1. Разделение строки на элементы
- 2. Структурирование в словарь (json-like)
- 3. Разработка структуры классов
- 4. Инициализация функции по строке через словарь элементов'''
- omega1 = map (gp.Variable,["x","y","z"])
- print omega1
- import re
- import json
- import math
- '''Идея работы парсера состоит в последовательном разбивании входящей функции по операторам на операнды.
- В числе операторов содержатся также скобки, поэтому разбивание происходит до нахождения атомарных переменных.
- Пример результата работы:
- {
- "function":
- {
- "name":"root_function",
- "elements":
- {
- "(":"open_bracket",
- "function":
- {
- "name":"sin",
- "elements":
- {
- "(":"open_bracket",
- "x":"atomic_function",
- "+":"operator",
- "x^2":"atomic_function",
- "-":"operator",
- 2:"constant",
- ")":"close_bracket"
- }
- },
- ")":"close_bracket"
- }
- }
- }
- Open and close brackets are flags for initialization of Function object in code.
- If we got expression in brackets without calling a function, we still initialize it as Function object.
- After the initialization call for ElementaryFunction object it separates on operators like
- It is explicit example of JSON-structure of parsed string. After writing of OOP-module, there will
- Идея по структуре наследования:
- (Constant & Variable) -> AtomicFunction or ElementaryFunction -> Function
- '''
- #Service dictionaries
- entityDict = {
- ')':'open_bracket',
- '(':'close_bracket',
- '+':'operator',
- '-':'operator',
- '*':'operator',
- '\\':'operator',
- '/':"operator",
- '\w':'variable' }
- functionDict = {
- 'sin':math.sin
- }
- #Separation block
- testString = "(sin(x))^2+cos^2(x+1)+x+x^2+10";
- bracketSplitList = re.split('(\(|\))',testString)
- operatorSplitList = [];
- for i in range (0,len(bracketSplitList)):
- splittedIterList = (re.split ('(\+|\-|\*|\/)',bracketSplitList[i]))
- for j in range(0,len(splittedIterList)):
- operatorSplitList.append(splittedIterList[j])
- #print operatorSplitList
- #Structuring block
- for i in range (0,len(operatorSplitList)):
- if ((operatorSplitList[i]=='')||(operatorSplitList[i]==' '):
- operatorSplitList.remove(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement