I have a voting system for something where people rate things from one to ten. I am able to get the number of total votes, but not their distribution. I can, however, access the totals for each score (but not what score they are for).
For example, say two people voted 2 and three 4 then I would get [4, 12]. Humanly, it is possibly to work out what the votes were, however as there is a lot of data, I need an algorithm that is able to do it programmatically. To solve this humanly, I would know that there were 5 votes and this I would know that it was either one four and four thees or two twos and three fours. Because this isn't the best example, we can't tell which one it would be and so it this situation, the algorithm should spit out all of the solutions. With the real data, it is very rare that something like this occurs.
The input is in the form of an int representing the total number of votes cast, and a list of all of the vote subtotals. The output should be an array containing pars (x, y) where there are x number of y votes cast. If more than one solution is present, the algorithm should return all of them in an array.
EDIT:
Here's some real data (17 people voted):
Dunkel - 60 + 18 + 8 + 18 + 10 + 4 + 3 + 1 = 122
Satomi - 20 + 14 + 24 + 12 + 3 + 4 + 3 = 80
Bottersnike - 16 + 28 + 5 + 8 + 6 + 4 + 4 = 71
Woon - 40 + 36 + 8 + 21 + 5 + 16 = 126
Limelier - 10 + 18 + 6 + 15 + 8 + 4 + 6 = 67
RandomGamer - 16 + 6 + 10 + 4 + 6 + 4 + 7 = 53
Pillar - 10 + 8 + 21 + 6 + 15 + 4 + 9 + 4 + 2 = 79
EdgedPixel - 8 + 28 + 12 + 4 + 18 + 2 + 2 = 74
Lock - 20 + 24 + 7 + 18 + 10 + 8 + 6 + 2 = 95
Huri - 10 + 8 + 7 + 6 + 15 + 20 + 3 + 2 + 3 = 74
Sean - 18 + 32 + 8 + 5 + 4 + 9 + 2 = 78
And the answers (sorry about the different order):
Woon - 4*10 + 4*9 + 1*8 + 3*7 + 0*6 + 1*5 + 4*4 + 0*3 + 0*2 + 0*1 = 126
Dunkel - 6*10 + 2*9 + 1*8 + 0*7 + 3*6 + 2*5 + 1*4 + 1*3 + 0*2 + 1*1 = 122
Lock - 2*10 + 0*9 + 3*8 + 1*7 + 3*6 + 2*5 + 2*4 + 2*3 + 0*2 + 2*1 = 95
Satomi - 2*10 + 0*9 + 3*8 + 4*7 + 2*6 + 0*5 + 0*4 + 1*3 + 2*2 + 3*1 = 80
Pillar - 1*10 + 0*9 + 1*8 + 3*7 + 1*6 + 3*5 + 1*4 + 3*3 + 2*2 + 2*1 = 79
Sean - 0*10 + 0*9 + 4*8 + 0*7 + 3*6 + 1*5 + 2*4 + 3*3 + 2*2 + 2*1 = 78
EdgedPixel - 0*10 + 0*9 + 1*8 + 4*7 + 2*6 + 0*5 + 1*4 + 6*3 + 1*2 + 2*1 = 74
Huri - 1*10 + 0*9 + 1*8 + 1*7 + 1*6 + 3*5 + 5*4 + 1*3 + 1*2 + 3*1 = 74
Bottersnike - 0*10 + 0*9 + 2*8 + 4*7 + 0*6 + 1*5 + 2*4 + 2*3 + 2*2 + 4*1 = 71
Limelier - 1*10 + 2*9 + 0*8 + 0*7 + 1*6 + 3*5 + 2*4 + 0*3 + 2*2 + 6*1 = 67
RandomGamer - 0*10 + 0*9 + 2*8 + 0*7 + 1*6 + 2*5 + 1*4 + 2*3 + 2*2 + 7*1 = 53
I need an algorithm that is able to do it programmatically
I'd begin by factoring your totals.
What is the most efficient way of finding all the factors of a number in Python?
def factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
This will return all of the factors, very quickly, of a number n.
that would get you well on your way; obviously a listed total that doesn't have 5 for a factor did not come from a group that rated 5's
step 1) factor each number in your list of totals
step 2) create dictionary with all possible ratings as the keys; ie keys 1-10; each with empty lists as values.
step 3) check if each list of factors has a factor which matches each rating key; if so append the total it represents to the values list of possibilities for that respective key
from random import randint
from random import shuffle
VOTES = 200
RATINGS = 10
# Generate Test Data
ratings = {}
for i in range(1,(RATINGS+1)):
ratings[i] = []
for i in range(VOTES):
rate = randint(1,RATINGS)
ratings[rate].append(rate)
subtotals = []
for key, values in ratings.iteritems():
subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()
hidden_subtotals = {}
for key, values in ratings.iteritems():
hidden_subtotals[key] = sum(values)
print '==================================='
print 'Generate Hidden Test Data'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals
print '==================================='
print 'Present Problem'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), RATINGS)
print '==================================='
def factors(n):
f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
1, int(n**0.5) + 1) if n % i == 0))))
f.sort()
return f
# Factor Each
subtotal_factors = {}
for i in subtotals:
subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors
# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
possible_ratings[i] = [
z for z in subtotal_factors[i] if z<=RATINGS]
print 'process 2: %s' % possible_ratings
# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
possible_ratings[i] = [
z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings
step 4) then you'll have a problem where 2's will fit in 4's, 6's, 8's, 10's; 4's will fit in 8's, and 3's will fit in 6's and 9's. You'll have to apply some logic to clean up the results.
I have now solved my problem. I used litepresence's code as a base, and then implemented what they described as step four. For the future, this is the code I used:
from random import randint
from random import shuffle
import itertools
NUM_RATINGS = 10
RATING = [10, 10, 10, 10, 10, 10, 9, 9, 8, 6, 6, 6, 5, 5, 4, 3, 1]
VOTES = len(RATING)
ratings = {}
for i in range(1, (NUM_RATINGS + 1)):
ratings[i] = []
for i in RATING:
ratings[i].append(i)
subtotals = []
for key, values in ratings.iteritems():
subtotals.append(sum(values))
subtotals = [i for i in subtotals if i != 0]
subtotals.sort()
hidden_subtotals = {}
for key, values in ratings.iteritems():
hidden_subtotals[key] = sum(values)
print '==================================='
print 'Hidden Test Data:'
print 'unknown actual votes: %s' % ratings
print 'unknown actual subtotals: %s' % hidden_subtotals
print '==================================='
print 'Present Problem:'
print 'known vote total: %s' % VOTES
print 'known subtotals: %s' % subtotals
print 'known total: %s' % sum(subtotals)
print 'number of ratings used: %s of %s' % (len(subtotals), NUM_RATINGS)
print '==================================='
def factors(n):
f = list(set(reduce(list.__add__, ([i, n//i] for i in range(
1, int(n**0.5) + 1) if n % i == 0))))
f.sort()
return f
# Factor Each
subtotal_factors = {}
for i in subtotals:
subtotal_factors[i]=(factors(i))
print 'process 1: %s' % subtotal_factors
# Remove Factors greater than highest rating
possible_ratings = {}
for i in subtotal_factors:
possible_ratings[i] = [
z for z in subtotal_factors[i] if z<=NUM_RATINGS]
print 'process 2: %s' % possible_ratings
# Remove Factors if not enough votes for that possibility; too small relative to subtotal
for i in possible_ratings:
possible_ratings[i] = [
z for z in possible_ratings[i] if i/z < VOTES]
print 'process 3: %s' % possible_ratings
end_res = {}
other = {} # (count, [poss])
for i in possible_ratings.items():
if len(i[1]) == 1:
end_res[i[0]] = (i[1][0], i[1][0])
else:
other[i[0]] = (subtotals.count(i[0]), i[1])
combs = {}
for i in other.items():
combs[i[0]] = (i[1][0], []) # (count, [data])
for a in i[1][1]:
for b in i[1][1]:
if (a, b) not in combs[i[0]]:
if a * b == i[0]:
combs[i[0]][1].append((a, b))
lists = []
for i in combs.items():
for j in range(i[1][0]):
lists.append([])
for n in i[1][1]:
lists[-1].append((n[0], n[1], i[0]))
toprocess = itertools.product(*lists)
working = []
for test in toprocess:
works = True
seen = []
tot = 0
for i in test:
if i[1] in seen:
works = False
break
tot += i[0]
if tot > VOTES:
works = False
break
seen.append(i[1])
if not works:
continue
else:
working.append(test)
formattedWs = []
for w in working:
w = list(w)
notseen = [i + 1 for i in range(NUM_RATINGS)]
for i in w:
notseen.remove(i[1])
for i in notseen:
w.append((0, i))
t = ""
def f(x, y):
return y[1] - x[1]
w.sort(cmp=f)
for i in w:
t += "%d*%d + " % (i[0], i[1])
t = t[:-3]
formattedWs.append(t)
seen = []
for w in list(formattedWs):
if w in seen:
formattedWs.remove(w)
else:
seen.append(w)
print "==================================="
for n, w in enumerate(formattedWs):
print "Solution #%d: %s" % (n + 1, w)
Related
While trying to solve this problem using PSO algorithm I got the error message as "IndexError: invalid index to scalar variable". Since I am new to PSO algorithm, I am unable to figure out the error. Kindly help me to solve the issue.
import random
import numpy as np
import math
import matplotlib.pyplot as plt
def fitness_function(position):
s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
return s1+s2
#Some variables to calculate the velocity
W = 0.5
c1 = 0.8
c2 = 0.9
target = 1
n_iterations = int(input("Inform the number of iterations: "))
target_error = float(input("Inform the target error: "))
n_particles = int(input("Inform the number of particles: "))
particle_position_vector=np.array([np.random.uniform(low=0.025, high=0.035) for i in range (n_particles)])
print(particle_position_vector)
pbest_position = particle_position_vector
pbest_fitness_value = float('inf')
gbest_fitness_value = float('inf')
gbest_position = np.array([float('inf')for _ in range(n_particles)])
velocity_vector =np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
print(velocity_vector)
iteration = 0
while iteration < n_iterations:
for i in range(n_particles):
fitness_cadidate = fitness_function(particle_position_vector[i])
print(fitness_cadidate, ' ', particle_position_vector[i])
if(pbest_fitness_value[i] > fitness_cadidate):
pbest_fitness_value[i] = fitness_cadidate
pbest_position[i] = particle_position_vector[i]
if(gbest_fitness_value > fitness_cadidate):
gbest_fitness_value = fitness_cadidate
gbest_position = particle_position_vector[i]
if(abs(gbest_fitness_value - target) < target_error):
break
for i in range(n_particles):
new_velocity = (W*velocity_vector[i]) + (c1*random.random()) * (pbest_position[i] - particle_position_vector[i]) + (c2*random.random()) * (gbest_position-particle_position_vector[i])
new_position = new_velocity + particle_position_vector[i]
particle_position_vector[i] = new_position
iteration = iteration + 1
print("The best position is ", gbest_position, "in iteration number ", iteration)```
```IndexError Traceback (most recent call last)
<ipython-input-35-5610603d3302> in <module>
32 while iteration < n_iterations:
33 for i in range(n_particles):
---> 34 fitness_cadidate = fitness_function(particle_position_vector[i])
35 print(fitness_cadidate, ' ', particle_position_vector[i])
36
<ipython-input-35-5610603d3302> in fitness_function(position)
5
6 def fitness_function(position):
----> 7 s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
8 s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
9 return s1+s2
IndexError: invalid index to scalar variable.
Moreover, I want to plot a graph of fitness value with the iteration numbers. Please help me.
So I've created a very... odd little caesar cipher using python. Very simple. Except, I'm not really all that great at math and am wondering how I'd reverse this?
def encrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if (char.isupper()):
result += chr((ord(char) + s - 23+213**3) % 26 + 713)
else:
result += chr((ord(char) + s - 23+213**3) % 26 + 715)
return result
text = input("Message: ")
s = 964
print ("Text: " + text)
print ("Shift: " + str(s))
print ("Cipher: " + encrypt(text,s))
Any form of help would be appreciated.
Edit:
I'M SO CLOSE!
I did that math as to how the shift works:
if the letter is a capital:
1. 964 - 23+213^3, which ends up as -9662656
2. Get the remainder of that divided by 26 (modulo operator) -9662656 % 26 = 10
3. 10 + 714 = 724
4. 724-63 I got the 63 just through trial and error...
ONLY PROBLEM!
It all works up until the letter M, in which case the last 13 letters shift backward 26 characters. How would I solve this?
def decrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
result += chr((ord(char) - s))
return result
text = input("Message: ")
s = 724-63
print ("Text: " + text)
print ("Shift: " + str(s))
print ("Cipher: " + decrypt(text,s))
Text: ˖˗˘˙˚˛˜˝˞˟ˠˡˢˉˊˋˌˍˎˏːˑ˒˓˔˕
Shift: 661
Cipher: ABCDEFGHIJKLM456789:;<=>?#
I rewrote your encrypt function to show the individual piece of the calculation. Then I wrote the decrypt function to show how to "undo" them.
Note that -23+2133 is equivalent to 24 when working mod 26.
def encrypt(text, s):
cipher = ""
for pt_char in text:
val = ord(pt_char) # 65 <= val < 91
t0 = val % 26 # 0 <= t0 < 26, with A=>13, B=>14, ... N=>0 etc.
t1 = (t0 + s + 24) % 26 # 0 <= t1 < 26
t2 = t1 + 715 # 715 <= t2 < 741
cipher += chr(t2)
return cipher
def decrypt(ct, s):
plain = ""
for ct_char in ct:
t2 = ord(ct_char) # 715 <= t2 < 741
t1 = t2 - 715 # 0 <= t1 < 26
t0 = (t1 - s - 24 + 13) % 26 # 0 <= t0 < 26
val = t0 + 65 # 65 <= val < 91
plain += chr(val)
return plain
Again, this only works for upper case ASCII letters. Pay attention to the comments, they are telling you something.
Here are shorter one-liner just to show you something about how generators can produce compact code.
def encrypt_one_liner(text, s):
return ''.join(chr((ord(x) + s + 24) % 26 + 715) for x in text)
def decrypt_one_liner(text, s):
return ''.join(chr((ord(x) + 2 - s) % 26 + 65) for x in text)
I can only create a filled diamond, i can't figure out how to get it unfilled.`#
# The size of the diamond
N = 7
# The top part (includes the middle row)
i = 0
while i < N // 2 + 1:
print((N // 2 - i) * " " + (2 * i + 1) * "*")
i += 1
# The bottom part
i = 0
while i < N // 2:
print(" " * (i + 1) + "*" * (N - 2 - 2 * i))
i += 1
You just need to print (2*i-1) spaces in between '*' characters instead instead of only '*'. And have to deal with the very top and very bottom separately:
# The size of the diamond
N = 7
# The top part (includes the middle row)
print((N // 2) * " " + '*')
i = 1
while i < N // 2 + 1:
print((N // 2 - i) * " " + '*' + (2 * i - 1) * " " + '*')
i += 1
# The bottom part
i = 0
while i < N // 2 - 1:
print(" " * (i + 1) + '*' + " " * (N - 4 - 2 * i) + '*')
i += 1
print((N // 2) * " " + '*')
*
* *
* *
* *
* *
* *
*
# The size of the diamond
N = 7
# The top part (includes the middle row)
i = 0
while i < N // 2 + 1:
print((N // 2 - i) * " " + "*" +( ((2 * i -1) * " " + "*") if i > 0 else ""))
i += 1
# The bottom part
i = 0
while i < N // 2:
print(" " * (i + 1) + "*" + ( ( " " * (N - 2 - 2 * i - 2) + "*") if i < (N//2-1) else ""))
i += 1
simply print two less spaces than you were printing *'s inbetween two *'s unless on top or bottom.
def diamond(size, sym_func):
s = ''
for row in xrange(size):
for column in xrange(size):
if row > size//2: # if bottom half reflect top
row = size - row - 1
if column > size//2: # if right half reflect left
column = size - column - 1
s += sym_func(row,column,size)
s+= '\n'
return s
def solid(row,column,size):
if column >= (size // 2 - row):
return "*"
return " "
def hollow(row,column,size):
if column == (size // 2 - row):
return "*"
return " "
def circle(row,column,size):
if (size//2-row)**2+(size//2-column)**2 <= (size//2)**2:
return '*'
return ' '
print diamond(size=7, sym_func=solid) # The size of the diamond
print diamond(size=7, sym_func=hollow) # The size of the diamond
print diamond(size=17, sym_func=circle) # The size of the diamond
look at the difference between the hollow and solid symbol functions if you use a >= then you get a solid thing if you use a == for exact comperison then it is just the parimiter
The function does what I want it to, but when it's done it just sits there rather than continuing from where I called it and I can't figure out why. The code is:
x = 9
y = 9
n = 10
ty = 1
tx = 1
while ty <= y:
while tx <= x:
vars()["p" + str(ty) + str(tx)] = 0
tx += 1
ty += 1
tx = 1
tn = 1
while tn <= n:
vars()["m" + str(tn)] = tn
tn += 1
t = x * y
tm = n
def recursion(z):
global tm
if z < n:
for x in range(n):
recursion(z + 1)
else:
if tm > 0:
tv = "m" + str(tm)
otm = eval(tv)
while eval(tv) < t - n + tm:
vars()[tv] = eval(tv) + 1
print(tv + " = " + str(eval(tv)))
vars()[tv] = otm + 1
print(tv + " = " + str(eval(tv)))
if tm > 1:
vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1
print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1)))))
tm -= 1
recursion(1)
print("done")
I've put the return in where I would expect it to end but as far as I know it shouldn't actually need it.
Can anyone see what I've done to cause it to get stuck?
Thanks
Note for people not going through the change history: This is based on the comments on other answers. UPDATE: Better version.
import itertools
def permutations(on, total):
all_indices = range(total)
for indices in itertools.combinations(all_indices, on):
board = ['0'] * total
for index in indices:
board[index] = '1'
yield ''.join(board)
If anyone is interested in what the original code did, I rearranged the conditionals to prune the tree of function calls:
x = 9
y = 9
n = 10
ty = 1
tx = 1
while ty <= y:
while tx <= x:
vars()["p" + str(ty) + str(tx)] = 0
tx += 1
ty += 1
tx = 1
tn = 1
while tn <= n:
vars()["m" + str(tn)] = tn
tn += 1
t = x * y
tm = n
def recursion(z):
global tm
if tm > 0:
if z < n:
for x in range(n):
recursion(z + 1)
else:
tv = "m" + str(tm)
otm = eval(tv)
while eval(tv) < t - n + tm:
vars()[tv] = eval(tv) + 1
print(tv + " = " + str(eval(tv)))
vars()[tv] = otm + 1
print(tv + " = " + str(eval(tv)))
if tm > 1:
vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1
print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1)))))
tm -= 1
recursion(1)
print("done")
This could be made much clearer through the use of lists and range objects, but that takes effort.
I wasn't able to work out what was happening (turns out if I left it for a few minutes it would actually finish though), instead, I realised that I didn't need to use recursion to achieve what I wanted (and I also realised the function didn't actually do what I want to do).
For anyone interested, I simplified and rewrote it to be a few while loops instead:
x = 9
y = 9
t = x * y
n = 10
tt = 1
while tt <= t:
vars()["p" + str(tt)] = 0
tt += 1
tn = 1
while tn <= n:
vars()["m" + str(tn)] = tn
vars()["p" + str(tn)] = 1
tn += 1
def cl():
w = ""
tt = 1
while tt <= t:
w = w + str(eval("p" + str(tt)))
tt += 1
p.append(w)
tm = n
tv = "m" + str(tm)
p = []
while m1 < t - n + tm - 1:
cl()
while tm == n and eval(tv) < t - n + tm:
vars()["p" + str(eval(tv))] = 0
vars()[tv] = eval(tv) + 1
vars()["p" + str(eval(tv))] = 1
cl()
tm -= 1
tv = "m" + str(tm)
while tm < n and tm > 0:
if eval(tv) < t - n + tm:
vars()["p" + str(eval(tv))] = 0
vars()[tv] = eval(tv) + 1
vars()["p" + str(eval(tv))] = 1
while tm < n:
tm += 1
ptv = tv
tv = "m" + str(tm)
vars()["p" + str(eval(tv))] = 0
vars()[tv] = eval(ptv) + 1
vars()["p" + str(eval(tv))] = 1
else:
tm -= 1
tv = "m" + str(tm)
I'm going to find $I_m=\int_0^{2\pi} \prod_{k=1}^m cos(kx){}dx$, where $m=1,2,3\ldots$
Simple SAGE code:
x=var('x')
f = lambda m,x : prod([cos(k*x) for k in range(1,m+1)])
for m in range(1,15+1):
print m, numerical_integral(f(m,x), 0, 2*pi)[0],integrate(f(m,x),x,0,2*pi).n()
Output:
1 -1.47676658757e-16 0.000000000000000
2 -5.27735962315e-16 0.000000000000000
3 1.57079632679 1.57079632679490
4 0.785398163397 0.785398163397448
5 -2.60536121164e-16 0.000000000000000
6 -1.81559273097e-16 0.000000000000000
7 0.392699081699 0.392699081698724
8 0.343611696486 0.147262155637022
9 -1.72448482421e-16 0.294524311274043
10 -1.8747663502e-16 0.196349540849362
11 0.214757310304 0.312932080728671
12 0.190213617698 0.177941771394734
13 -1.30355375996e-16 0.208621387152447
14 -1.25168280013e-16 0.0859029241215959
15 0.138441766107 0.134223318939994
As you can see numerical answer is right, but result of integrate(...) is right for $m=1,2,\ldots,7$ and then there is some bug.
We can print indefinite integral:
for m in range(7,11+1):
print 'm=',m
print 'Indef_I_m=',integrate(f(m,x),x)
And Output:
m = 7
Indef_I_m = 1/16*x + 1/16*sin(2*x) + 1/32*sin(4*x) + 7/384*sin(6*x) +
7/512*sin(8*x) + 3/320*sin(10*x) + 5/768*sin(12*x) + 5/896*sin(14*x) +
1/256*sin(16*x) + 1/384*sin(18*x) + 1/640*sin(20*x) + 1/704*sin(22*x) +
1/1536*sin(24*x) + 1/1664*sin(26*x) + 1/1792*sin(28*x)
m = 8
Indef_I_m = 3/128*x + 5/256*sin(2*x) + 1/32*sin(3*x) + 5/512*sin(4*x) +
5/768*sin(6*x) + 1/256*sin(8*x) + 1/256*sin(10*x) + 1/256*sin(12*x) +
1/256*sin(14*x) + 1/256*sin(16*x) + 7/2304*sin(18*x) + 3/1280*sin(20*x)
+ 5/2816*sin(22*x) + 1/768*sin(24*x) + 3/3328*sin(26*x) +
1/1792*sin(28*x) + 1/1920*sin(30*x) + 1/4096*sin(32*x) +
1/4352*sin(34*x) + 1/4608*sin(36*x) + 3/32*sin(x)
m = 9
Indef_I_m = 3/64*x + 3/128*sin(2*x) + 23/768*sin(3*x) + 3/256*sin(4*x) +
3/640*sin(5*x) + 1/128*sin(6*x) + 5/1792*sin(7*x) + 5/2304*sin(9*x) +
3/2816*sin(11*x) + 1/832*sin(13*x) + 1/1280*sin(15*x) + 3/4352*sin(17*x)
+ 5/4864*sin(19*x) + 1/1344*sin(21*x) + 3/2944*sin(23*x) +
7/6400*sin(25*x) + 1/1152*sin(27*x) + 3/3712*sin(29*x) +
5/7936*sin(31*x) + 1/2112*sin(33*x) + 3/8960*sin(35*x) +
1/4736*sin(37*x) + 1/4992*sin(39*x) + 1/10496*sin(41*x) +
1/11008*sin(43*x) + 1/11520*sin(45*x) + 23/256*sin(x)
m = 10
Indef_I_m = 1/32*x + 1/64*sin(2*x) + 17/512*sin(3*x) + 1/128*sin(4*x) +
7/2560*sin(5*x) + 1/192*sin(6*x) + 3/1792*sin(7*x) + 1/1152*sin(9*x) +
5/5632*sin(11*x) + 3/6656*sin(13*x) + 1/2560*sin(15*x) +
5/8704*sin(17*x) + 3/9728*sin(19*x) + 1/2688*sin(21*x) +
1/2944*sin(23*x) + 1/6400*sin(25*x) + 1/4608*sin(27*x) +
3/14848*sin(29*x) + 3/15872*sin(31*x) + 5/16896*sin(33*x) +
3/8960*sin(35*x) + 3/9472*sin(37*x) + 1/3328*sin(39*x) +
5/20992*sin(41*x) + 1/5504*sin(43*x) + 1/7680*sin(45*x) +
1/12032*sin(47*x) + 1/12544*sin(49*x) + 1/26112*sin(51*x) +
1/27136*sin(53*x) + 1/28160*sin(55*x) + 13/128*sin(x)
m = 11
Indef_I_m = 51/1024*x + 53/2048*sin(2*x) + 13/768*sin(3*x) + 53/4096*sin(4*x) +
13/1536*sin(6*x) + 1/2048*sin(8*x) + 1/2560*sin(10*x) + 1/3072*sin(12*x)
+ 5/14336*sin(14*x) + 1/4096*sin(16*x) + 5/18432*sin(18*x) +
1/4096*sin(20*x) + 1/5632*sin(22*x) + 5/24576*sin(24*x) +
5/26624*sin(26*x) + 5/28672*sin(28*x) + 1/5120*sin(30*x) +
3/16384*sin(32*x) + 5/34816*sin(34*x) + 1/9216*sin(36*x) +
5/38912*sin(38*x) + 1/10240*sin(40*x) + 1/10752*sin(42*x) +
3/22528*sin(44*x) + 3/23552*sin(46*x) + 1/8192*sin(48*x) +
3/25600*sin(50*x) + 5/53248*sin(52*x) + 1/13824*sin(54*x) +
3/57344*sin(56*x) + 1/29696*sin(58*x) + 1/30720*sin(60*x) +
1/63488*sin(62*x) + 1/65536*sin(64*x) + 1/67584*sin(66*x) +
13/256*sin(x)
so for $m=7$ answer is right compare with Indef_I_7 via WolframAlpha
and for $m=8$ answer is incorrect Indef_I_8 via WolframAlpha
There should be Indef_I_8=$\frac{7x}{128}+\ldots$ and no $\sin(x)$, $\sin(3x)$ in summation, only $\sin(2k)$ for $k=1,2,3,\ldots 18$
Sorry for volumetric calculations !
The question is - Am I right that it is the bug in the symbolic integration?
Well, apparently setting algorithm='mathematica_free' solved the issue; this is probably a bug in the default algorithm used bye SAGE ('maxima').
So the answer is - Yes. It is a bug in algorithm='maxima', so use algorithm='mathematica_free' (def new function to find definite integral) or simplify_full() for such product of cos(kx) and than integrate.
f(8,x).simplify_full().integral(x,0,2pi) == 7/64pi and
(7/64*pi).n() == 0.343611696486384 is correct