collect rational factors in expression - sympy
Suppose I have the following:
a, b, c = symbols('a b c')
test = a / 2 + b / 2 + c
pprint(collect(test, Rational(1, 2)))
a b
─ + ─ + c
2 2
Is there a way to get SymPy to return something more along the lines of 1 / 2 * (a + b) + c, or (a + b) / 2 + c?
Edit: collect_const() appears to work on my toy example, but doesn't seem to scale well to the real thing. Here is a better example.
a, b, c, d, e, f = symbols('a b c d e f')
test = c / a + d / a + e / (a * b) + f / (a * b)
print(test)
I'd like to obtain something along the lines of 1 / a * (c + d + 1 / b * (e + f)).
collect(test, (1 / b, 1 / a)) gives me (e/a + f/a)/b + (c + d)/a, which is close, but I'd like to recursively collect 1 / a in the first numerator. Furthermore, due to the nestedness of the expressions, it would be great to represent the denominators as a leading factor of e.g., 1 / a, that is 1 / a * (b + c) rather than (b + c) / a.
I think that you should use str() for this.
In you case,
str(collect(test, Rational(1, 2)))
gives output
'a/2 + b/2 + c'
For the exprerssion you mentioned, I would use
str(Add(Mul(Integer(-1), Add(Mul(Rational(1, 2), Symbol('f_2')), Mul(Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_5'), Add(Mul(Integer(-1), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_7'), Add(Mul(Integer(-1), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_8'), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1)))))))), Add(Mul(Rational(1, 2), Symbol('p_2')), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_8'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('a_2'), Integer(-1)), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_2'), Add(Mul(Symbol('a_1'), Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('a_2'), Integer(-1)), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_4'), Add(Mul(Symbol('a_1'), Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))))), Mul(Pow(Symbol('wBar'), Integer(-1)), Symbol('x_5'), Symbol('x_6'), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2'))), Mul(Pow(Symbol('wBar'), Integer(-1)), Pow(Symbol('x_6'), Integer(2)), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2'))), Mul(Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Symbol('x_8'), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')))))
This gives me output as
'-(f_2/2 + (a_2*(-f_2 + 1) + f_2)*(x_5*(-t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar) + x_6*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar) + x_7*(-t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar) + x_8*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar)))*(p_2/2 + x_6*(a_2*(-f_2 + 1) + f_2)*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar) + x_8*(a_2*(-f_2 + 1) + f_2)*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*wBar) + x_2*(a_1*a_2*(-f_2 + 1) + f_2)*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*a_2*wBar) + x_4*(a_1*a_2*(-f_2 + 1) + f_2)*(-o*t_2*z_2*(s - 1) + z_1*(-t_2 + 1))/(2*a_2*wBar)) + x_5*x_6*z_1*(a_2*(-f_2 + 1) + f_2)/wBar + x_6**2*z_1*(a_2*(-f_2 + 1) + f_2)/wBar + x_6*x_8*z_1*(a_2*(-f_2 + 1) + f_2)/wBar'
To get a simplified result I used simplify() as well.
str(simplify(Add(Mul(Integer(-1), Add(Mul(Rational(1, 2), Symbol('f_2')), Mul(Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_5'), Add(Mul(Integer(-1), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_7'), Add(Mul(Integer(-1), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_8'), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1)))))))), Add(Mul(Rational(1, 2), Symbol('p_2')), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_8'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('a_2'), Integer(-1)), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_2'), Add(Mul(Symbol('a_1'), Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))), Mul(Rational(1, 2), Pow(Symbol('a_2'), Integer(-1)), Pow(Symbol('wBar'), Integer(-1)), Symbol('x_4'), Add(Mul(Symbol('a_1'), Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2')), Add(Mul(Integer(-1), Symbol('o'), Symbol('t_2'), Symbol('z_2'), Add(Symbol('s'), Integer(-1))), Mul(Symbol('z_1'), Add(Mul(Integer(-1), Symbol('t_2')), Integer(1))))))), Mul(Pow(Symbol('wBar'), Integer(-1)), Symbol('x_5'), Symbol('x_6'), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2'))), Mul(Pow(Symbol('wBar'), Integer(-1)), Pow(Symbol('x_6'), Integer(2)), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2'))), Mul(Pow(Symbol('wBar'), Integer(-1)), Symbol('x_6'), Symbol('x_8'), Symbol('z_1'), Add(Mul(Symbol('a_2'), Add(Mul(Integer(-1), Symbol('f_2')), Integer(1))), Symbol('f_2'))))))
which gave me
'-(a_2*wBar*x_6*z_1*(a_2*(f_2 - 1) - f_2)*(x_5 + x_6 + x_8) + (f_2*wBar + (a_2*(f_2 - 1) - f_2)*(x_5*(t_2*z_2*(s - 1) + z_1*(t_2 - 1)) + x_6*(o*t_2*z_2*(s - 1) + z_1*(t_2 - 1)) + x_7*(t_2*z_2*(s - 1) + z_1*(t_2 - 1)) + x_8*(o*t_2*z_2*(s - 1) + z_1*(t_2 - 1))))*(a_2*p_2*wBar + a_2*(x_6 + x_8)*(a_2*(f_2 - 1) - f_2)*(o*t_2*z_2*(s - 1) + z_1*(t_2 - 1)) + (x_2 + x_4)*(a_1*a_2*(f_2 - 1) - f_2)*(o*t_2*z_2*(s - 1) + z_1*(t_2 - 1)))/4)/(a_2*wBar**2)'
Is this the expected result?
Related
Create partial sublists by index, discarding some data [(x,y,z),...] to [(x,y),...]
I got a list1 which has N items in each item it has (x,y,z). For z in list2, I need to create a new list which has items from list1 with only (x,y) this need to be done for list3, list4 too. list1 = [(1250, 1442, 0), (1280, 1655, 1), (1029, 1680, 2), (624, 1573, 3), (732, 1159, 4), (1530, 1634, 5), (1885, 1628, 6), (2152, 1834, 7), (1252, 2459, 8), (1309, 3023, 9), (1376, 3585, 10), (1571, 2388, 11), (1682, 2952, 12), (1686, 3579, 13), (1184, 1391, 14), (1291, 1382, 15), (1117, 1440, 16), (1361, 1400, 17)] list2 = [0,1,14,15,16,17] list3 = [2,3,4,5,6,7,8,11] list4 = [9,10,12,13] For example, list5 which is between list1 and list2 looks like list5 = [(1250, 1442),(1280, 1655),(1184, 1391)......] Can anyone suggest a fast way to do it? Thank you
Easy enough: def getXYfromIndex(l, indexes): """Returns x,y from bigger list 'l' containing (x,y,z). Uses only those elements (by index) of 'l' that are in 'indexes'""" # list comprehension: returns x,y for each index in 'l' that is in 'indexes' return [(x,y) for x,y,_ in (l[i] for i in indexes)] list1 = [(1250, 1442, 0), (1280, 1655, 1), (1029, 1680, 2), (624, 1573, 3), (732, 1159, 4), (1530, 1634, 5), (1885, 1628, 6), (2152, 1834, 7), (1252, 2459, 8), (1309, 3023, 9), (1376, 3585, 10), (1571, 2388, 11), (1682, 2952, 12), (1686, 3579, 13), (1184, 1391, 14), (1291, 1382, 15), (1117, 1440, 16), (1361, 1400, 17)] list2 = [0,1,14,15,16,17] list3 = [2,3,4,5,6,7,8,11] list4 = [9,10,12,13] print(getXYfromIndex(list1,list2)) # use list5 = getXYfromIndex(list1,list2) print(getXYfromIndex(list1,list3)) # to work with those (x,y) - I just print them print(getXYfromIndex(list1,list4)) Output: [(1250, 1442), (1280, 1655), (1184, 1391), (1291, 1382), (1117, 1440), (1361, 1400)] [(1029, 1680), (624, 1573), (732, 1159), (1530, 1634), (1885, 1628), (2152, 1834), (1252, 2459), (1571, 2388)] [(1309, 3023), (1376, 3585), (1682, 2952), (1686, 3579)]
Python: In List, adding counts of tuples with same string name together
In my list L below, how can I add "alex" together, so 19+ 12=21 count for alex. For mike, how can I add to produce single (mike, 30) tuple from these 2 tuples ('mike / 213880_at', 15), ('mike / 210393_at', 15), Same for marko: how to get (marko, 24) from the tuples below inside the list: ('marko / 229481_at', 12), ('marko / 1553115_at', 12), L = [ ('alex / 222696_at', 19), ('mike / 213880_at', 15), ('mike / 210393_at', 15), ('NOTUM / 228649_at', 13), ('marko / 229481_at', 12), ('marko / 1553115_at', 12), ('alex / 224176_s_at', 12), ('pocke / 227812_at', 11), ('locke / A_23_P111995', 10), ('nicko / 225016_at', 10), ('kolya / A_23_P122216', 10), ('raj / 211518_s_at', 9), ('dave / 240211_at', 8) ]
You can do it this way: L = [ ('alex / 222696_at', 19), ('mike / 213880_at', 15), ('mike / 210393_at', 15), ('NOTUM / 228649_at', 13), ('marko / 229481_at', 12), ('marko / 1553115_at', 12), ('alex / 224176_s_at', 12), ('pocke / 227812_at', 11), ('locke / A_23_P111995', 10), ('nicko / 225016_at', 10), ('kolya / A_23_P122216', 10), ('raj / 211518_s_at', 9), ('dave / 240211_at', 8) ] new = {} for name, number in L: val = name.split(' / ')[0] if val in new: new[val] += number else: new[val]= number print(new) Output: {'alex': 31, 'mike': 30, 'NOTUM': 13, 'marko': 24, 'pocke': 11, 'locke': 10, 'nicko': 10, 'kolya': 10, 'raj': 9, 'dave': 8} If you wanted back in a tuple: #add this new = tuple([(x, new[x]) for x in new])
If you wanted to retain list order and have O(1) look-ups. from collections import OrderedDict def add_pairs(sequence): container = OrderedDict() for tag, value in sequence: key = tag.split()[0] container[key] = container.get(key, 0) + value return container Example: >>> L = [ ('alex / 222696_at', 19), ('mike / 213880_at', 15), ('mike / 210393_at', 15), ('NOTUM / 228649_at', 13), ('marko / 229481_at', 12), ('marko / 1553115_at', 12), ('alex / 224176_s_at', 12), ('pocke / 227812_at', 11), ('locke / A_23_P111995', 10), ('nicko / 225016_at', 10), ('kolya / A_23_P122216', 10), ('raj / 211518_s_at', 9), ('dave / 240211_at', 8) ] >>> add_pairs(L) OrderedDict([('alex', 31), ('mike', 30), ('NOTUM', 13), ('marko', 24), ('pocke', 11), ('locke', 10), ('nicko', 10), ('kolya', 10), ('raj', 9), ('dave', 8)])
Custom Layers for recursive interation bitween layers
I have a question. I'm trying to make a CNN, the return of model feeds first Dence layer. Can I help? e.g: model = Sequential() model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=X_train.shape[1:])) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) I Should have the output value feeds layer Dense (512) Dense (512 + 10, name='d1') output d2 with input here. Activation() Dropout(522) Dense (10, name='d2')
Alexa built-in slot AMAZON.DATE
When using the built-in slot AMAZON.DATE, "next week" is interpreted as 2016-W38 per the developer reference. Utterances that map to just a specific week (such as “this week” or “next week”), convert a date indicating the week number: 2015-W49. I am trying to parse this in SQL. Is the first week of the year W1 or W01 when using the Alexa services? Thanks.
UPDATED: Dates are in ISO-8601 date format. First week is W01, there is no W00. Reference: https://en.wikipedia.org/wiki/ISO_8601#Dates Source: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interaction-model-reference Original answer: I can't find docs, but here is the answer... Today is Wed 28th sept, day 272. Alexa says this is 2016-W39. (I just checked). 2016 started on a Friday, so W39 started last Friday on day 267 which is 38 weeks after Jan 1st. (267/7=38.14). Count back 38 weeks to start of year. Therefore, first week of the year is called W1.
This is how I ended up parsing the AMAZON.DATE built-in slot using a SQL Server stored procedure. CREATE PROCEDURE [dbo].[procMomaAlexaExhibitions] #p_alexa_date VARCHAR(50) = '' AS BEGIN SET NOCOUNT ON; DECLARE #p_start_date VARCHAR(10) = '' DECLARE #p_end_date VARCHAR(10) = '' --SET #p_alexa_date = '2015-W20-WE' -- Today if null IF(#p_alexa_date = '' OR #p_alexa_date IS NULL) BEGIN SET #p_start_date = (SELECT CONVERT(VARCHAR(10), GETDATE(), 121)) SET #p_end_date = (SELECT CONVERT(VARCHAR(10), GETDATE(), 121)) END -- Alexa provided a normal date IF(ISDATE(#p_alexa_date) = 1) BEGIN SET #p_start_date = CONVERT(VARCHAR(10), #p_alexa_date, 121) SET #p_end_date = CONVERT(VARCHAR(10), #p_alexa_date, 121) END -- Alexa provided a year-month IF(LEN(#p_alexa_date) <= 8 AND ISNUMERIC(REPLACE(#p_alexa_date,'-','')) = 1) BEGIN SET #p_start_date = #p_alexa_date + '-01' SET #p_end_date = #p_alexa_date + '-31' END -- weekend IF(RIGHT(#p_alexa_date, 3) = '-WE') BEGIN DECLARE #p_week VARCHAR(2) DECLARE #p_year VARCHAR(4) DECLARE #p_start_date_wk VARCHAR(10) = '' DECLARE #p_end_date_wk VARCHAR(10) = '' SET #p_week = REPLACE(SUBSTRING(#p_alexa_date,7, LEN(#p_alexa_date)),'-WE','') SET #p_year = LEFT(#p_alexa_date, 4) SET #p_start_date_wk = CONVERT(VARCHAR(10), DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + #p_year) + (#p_week-1), 6), 121) SET #p_end_date_wk = CONVERT(VARCHAR(10), DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + #p_year) + (#p_week-1), 5), 121) SET #p_start_date = CASE WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime)) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime), 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 1) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 1, 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 2) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 2, 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 3) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 3, 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 4) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 4, 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 5) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 5, 121) WHEN datename(weekday, CAST(#p_start_date_wk AS smalldatetime) + 6) = 'Saturday' THEN CONVERT(VARCHAR(10), CAST(#p_start_date_wk AS smalldatetime) + 6, 121) END SET #p_end_date = CASE WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime)) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime), 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 1) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 1, 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 2) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 2, 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 3) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 3, 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 4) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 4, 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 5) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 5, 121) WHEN datename(weekday, CAST(#p_end_date_wk AS smalldatetime) + 6) = 'Sunday' THEN CONVERT(VARCHAR(10), CAST(#p_end_date_wk AS smalldatetime) + 6, 121) END END -- not a weekend IF((#p_alexa_date LIKE '%-W%') AND (RIGHT(#p_alexa_date, 3) <> '-WE')) BEGIN SET #p_week = SUBSTRING(#p_alexa_date,7, LEN(#p_alexa_date)) SET #p_year = LEFT(#p_alexa_date, 4) SET #p_start_date = CONVERT(VARCHAR(10), DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + #p_year) + (#p_week-1), 6), 121) SET #p_end_date = CONVERT(VARCHAR(10), DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + #p_year) + (#p_week-1), 5), 121) END From there you can do things like this in SQL. FYI WHERE (ExhVenuesXrefs.BeginISODate <= #p_start_date) AND (ExhVenuesXrefs.EndISODate >= #p_end_date)
SyntaxError: invalid syntax in python for math expression
i'm not used with python. just know the basic things. i'm using math operation in python for calculation of inverse jacobian which has super long character in one lines. my question is 'do the python has limit character in one line?' or how to make a right module in python? or which operation in math for python that not allowed? my python program is designed for ROS platform, and this is part of my program: class Skelplot(): def __init__(self): .... self.q_raw =[0,0,0] self.dq_ref =[0,0,0] .... while not rospy.is_shutdown(): ... dx = x_now - right_hand.x() dy = y_now - right_hand.y() dz = z_now - right_hand.z() self.inverse_jacobian(dx,dy,dz) self.q_raw[0] = self.dq_ref[0] + self.q_raw[0] self.q_raw[1] = self.dq_ref[1] + self.q_raw[1] self.q_raw[2] = self.dq_ref[2] + self.q_raw[2] ... def inverse_jacobian(self, dx, dy, dz): self.dq_ref[0] = (-(5*sin(self.q_raw[0] + 2*self.q_raw[1]) - 5*sin(self.q_raw[0] - self.q_raw[2]) + 5*sin(q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) + 10*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 5*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) - 15*sin(self.q_raw[0]) + 10*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((5*cos(self.q_raw[0] + 2*self.q_raw[1]) - 5*cos(self.q_raw[0] - self.q_raw[2]) + 5*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) + 10*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 5*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) - 15*cos(self.q_raw[0]) + 10*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy self.dq_ref[1] = (-(5*sin(self.q_raw[0] - self.q_raw[1]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1]) + 5*sin(self.q_raw[0] - self.q_raw[2]) - 5*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 5*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 10*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1]) + 5*sin(self.q_raw[0] + self.q_raw[2]) + 10*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((5*cos(self.q_raw[0] - self.q_raw[1]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1]) + 5*cos(self.q_raw[0] - self.q_raw[2]) - 5*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 5*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 10*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1]) + 5*cos(self.q_raw[0] + self.q_raw[2]) + 10*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy this is the respond: Traceback (most recent call last): File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 40, in <module> class Skelplot(): File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 203, in Skelplot self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy NameError: name 'self' is not defined i also tried with exception handling like this: .... def inverse_jacobian(self, dx, dy, dz): try: self.dq_ref[0] : .. self.dq_ref[1] : .. self.dq_ref[2] : .. except KeyError: pass ... and the response is : File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 204 self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy ^ SyntaxError: invalid syntax so, could you which one did i go wrong? thankyou.
I copied your code into my Python2.7 and it doesn't give me any error. I suppose it is due to that you didn't use a constant indentation, make sure you use tab or the same of space all the time. You may consider choosing a proper IDE such as Spyder or Pycharm, they will do all this for you.