How Python calculates % function can some one please explain 3%5 - python-2.7

How Python calculates % function? can some one please explain 3%5 outcome as 3 in Python? Answer for 5%3 is also showing 3. I use python 2.7

The Python % operator isn't percentage, it's modulo. That means the remainder part of a division. Remember when you were a kid and your math problems would be like 11 divided by 3 = 3 R 2 (remainder 2)? That's what % does. 5 % 3 = 2.
If you want to calculate percentage, do that yourself like A * 100.0 / B.

Related

How many FLOPs are there in calculating a factorial using math.factorial(n) in python

I am trying to understand how many FLOPs are there if I use a certain algorithm to find the exponential approximated sum, specially If I use math.factorial(n) in python. I understand FLOPs for binary operation, so is factorial also a binary operation here within a function? Not being a computer science major, I have some difficulties with these. My code looks like this:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math
x = input ("please enter a number for which you want to run the exponential summation e^{x}")
N = input ("Please enter an integer before which term you want to turncate your summation")
function= math.exp(x)
exp_sum = 0.0
abs_err = 0.0
rel_err = 0.0
for n in range (0, N):
factorial = math.factorial(n) #How many FLOPs here?
power = x**n # calculates N-1 times
nth_term = power/factorial #calculates N-1 times
exp_sum = exp_sum + nth_term #calculates N-1 times
abs_err = abs(function - exp_sum)
rel_err = abs(abs_err)/abs(function)
Please help me understand this. I might also be wrong about the other FLOPs!
According to that SO answer and to the C source code, in python2.7 math.factorial(n) uses a naive algorithm to compute the factorial so it computes using about n operations as factorial(n)=1*2*3*4*...*n.
A small mistake regarding the rest is that for n in range(0,N) will loop N times , not N-1 (from n=0 to n=N-1).
A final note is that counting FLOP may not be representative of the actual algorithm real world performance especially in python that is an interpretted language and that it tends to hide most of its inner working behind clever syntax that links to compiled C code(eg: exp_sum + nth_term is actualy exp_sum.__add__(nth_term)).

Linear Programming: How to implement with multiple constraints?

I’m trying to solve a linear programing model and need some help. I’m not a programming expert, but I conceptually can draw up the problem and am hoping for some help implementing it.
I’m looking into an asset allocation problem for an investment portfolio from a theoretical perspective, but for simplicity of this post I’m going to use generic terms.
I have a list of 500+ choices that all have an assigned cost and value add. My goal is to maximize the sum of the value add, given a constraint on how much I can spend. These 500 choices are divided into 5 categories and there are restrictions on how many choices I can have from each category.
Category 1 = 1
Category 2 = 1
Category 3 = 2 or 3
Category 4 = 1 or 2
Category 5 = 2
Category 3 + Category 4 = 4
I figure I’ll need to use a binary X variable attached to each choice and 1 means I’m picking that choice and 0 doesn’t so in the end there should be 8 variables that have 1 and the rest have a 0 value that leads to the maximum value add given the constraints on cost each choice has.
I ultimately hope to be able to run and say for example “what is the nth highest value” so instead of getting the maximum value add I can get the second highest value add and so on.
Is this possible and what software/language would be best to do it? Thanks for your help!
Just to simplify writing everything down, let's assume you had 15 assets, with value added v_1, v_2, ..., v_15 and costs c_1, c_2, ..., c_15. Let's assume assets 1, 2, and 3 are in category 1, assets 4, 5, and 6 are in category 2, assets 7, 8, and 9 are in category 3, assets 10, 11, and 12 are in category 4, and assets 13, 14, and 15 are in category 5. Finally, let's assume a budget B.
We would create binary variables x_1, x_2, ..., x_15 to indicate whether we bought each asset. Now, the objective function of our integer program is:
max v_1*x_1 + v_2*x_2 + ... + v_15*x_15
Our budget constraint is:
c_1*x_1 + c_2*x_2 + ... + c_15*x_15 <= B
Exactly one choice from category 1:
x_1 + x_2 + x_3 = 1
Exactly one choice from category 2:
x_4 + x_5 + x_6 = 1
Either 2 or 3 choices from category 3:
x_7 + x_8 + x_9 >= 2
x_7 + x_8 + x_9 <= 3
Either 1 or 2 choices from category 4:
x_10 + x_11 + x_12 >= 1
x_10 + x_11 + x_12 <= 2
Exactly 2 choices from category 5:
x_13 + x_14 + x_15 = 2
Exactly 4 choices from categories 3 and 4 combined:
x_7 + x_8 + x_9 + x_10 + x_11 + x_12 = 4
Finally, you would specify all variables to be binary.
Note that the only adjustment you would need to your problem is to change the variables in each of these constraints to be the variables associated with each of your five categories.
All that remains would be to implement the model. There are a myriad of linear programming packages in all major languages; check out this survey for details. Since Stack Overflow is not a software recommendation site and you haven't really given any details about your situation (e.g. free vs. non-free solvers or the programming language you're using), I will refrain from suggesting a particular package.

Why math.ceil() give different answers

I am new to python. I was trying to solve a matrix problem in which I have to use exit condition in loop for example if column and row of matrix is 3 or 4 then i want to run the loop 2 times and if col and row is 5 or 6 then it run 3 times.
>>> math.ceil(1.5)
2.0
>>> i=3
>>> math.ceil(i/2)
1.0
This is because 3 / 2 isn't 1.5 in Python 2, it's 1. Do from __future__ import division and then it'll be what you expect.
try this first:
i=3/2
print i
j=float(3)/2
print j
print math.ceil(j)
you should see
1
1.5
2.0
the way python deals with integer division is taking the lower bound.
Reference:
http://docs.python.org/2/reference/expressions.html

Computation of Kullback-Leibler (KL) distance between text-documents using numpy

My goal is to compute the KL distance between the following text documents:
1)The boy is having a lad relationship
2)The boy is having a boy relationship
3)It is a lovely day in NY
I first of all vectorised the documents in order to easily apply numpy
1)[1,1,1,1,1,1,1]
2)[1,2,1,1,1,2,1]
3)[1,1,1,1,1,1,1]
I then applied the following code for computing KL distance between the texts:
import numpy as np
import math
from math import log
v=[[1,1,1,1,1,1,1],[1,2,1,1,1,2,1],[1,1,1,1,1,1,1]]
c=v[0]
def kl(p, q):
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0,(p-q) * np.log10(p / q), 0))
for x in v:
KL=kl(x,c)
print KL
Here is the result of the above code: [0.0, 0.602059991328, 0.0].
Texts 1 and 3 are completely different, but the distance between them is 0, while texts 1 and 2, which are highly related has a distance of 0.602059991328. This isn't accurate.
Does anyone has an idea of what I'm not doing right with regards to KL? Many thanks for your suggestions.
Though I hate to add another answer, there are two points here. First, as Jaime pointed out in the comments, KL divergence (or distance - they are, according to the following documentation, the same) is designed to measure the difference between probability distributions. This means basically that what you pass to the function should be two array-likes, the elements of each of which sum to 1.
Second, scipy apparently does implement this, with a naming scheme more related to the field of information theory. The function is "entropy":
scipy.stats.entropy(pk, qk=None, base=None)
http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.stats.entropy.html
From the docs:
If qk is not None, then compute a relative entropy (also known as
Kullback-Leibler divergence or Kullback-Leibler distance) S = sum(pk *
log(pk / qk), axis=0).
The bonus of this function as well is that it will normalize the vectors you pass it if they do not sum to 1 (though this means you have to be careful with the arrays you pass - ie, how they are constructed from data).
Hope this helps, and at least a library provides it so don't have to code your own.
After a bit of googling to undersand the KL concept, I think that your problem is due to the vectorization : you're comparing the number of appearance of different words. You should either link your column indice to one word, or use a dictionnary:
# The boy is having a lad relationship It lovely day in NY
1)[1 1 1 1 1 1 1 0 0 0 0 0]
2)[1 2 1 1 1 0 1 0 0 0 0 0]
3)[0 0 1 0 1 0 0 1 1 1 1 1]
Then you can use your kl function.
To automatically vectorize to a dictionnary, see How to count the frequency of the elements in a list? (collections.Counter is exactly what you need). Then you can loop over the union of the keys of the dictionaries to compute the KL distance.
A potential issue might be in your NP definition of KL. Read the wikipedia page for formula: http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
Note that you multiply (p-q) by the log result. In accordance with the KL formula, this should only be p:
return np.sum(np.where(p != 0,(p) * np.log10(p / q), 0))
That may help...

coldfusion - rounding to nearest 5 cents

In coldfusion, how does one round a decimal to the nearest 5 cents? So that a figure of 0.39675 would round up to 0.40. And if the figure was 0.3690, it would round down to 0.35.
I can't seem to find anything useful via google.
Sorry for the brief question, but I think that's all I can really input.
Multiply by 20, round it, divide by 20:
RoundedNumber = ( Round( Number * 20 ) / 20 )