discrete-mathematics

how to minimize a function with discrete variable values in scipy

与世无争的帅哥 提交于 2019-11-29 06:56:24
I'm trying to optimize a target function that has multiple input variables (between 24 and 30). These variables are samples of three different statistical variables, and target function values are t-test probability values. An error function represents the error (sum of squares of differences) between the desired and the actual t-test probabilities. I can only accept solutions where the error is less than 1e-8, for all of the three t-tests. I was using scipy.optimize.fmin and it worked great. There are many solutions where the target function became zero. The problem is that I need to find a

Height of a tree with only one node

旧街凉风 提交于 2019-11-28 20:23:20
According to Wikipedia, The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a height of zero (or one). I dont get it - is it zero or one (or both)? It just an assuption you make for the recursive description of the height of a binary tree. You can consider a tree composed by just a node either with 0 height or with 1 height. If you really want to think about it somehow you can think that it's 0 if you consider the height as a edge count (so that a single node doesn't have any edge, hence 0) it's 1 if

De Bruijn-like sequence for `2^n - 1`: how is it constructed?

混江龙づ霸主 提交于 2019-11-28 16:51:56
I'm looking at the entry Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup from Bit Twiddling hacks . I can easily see how the second algorithm in that entry works static const int MultiplyDeBruijnBitPosition2[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 }; r = MultiplyDeBruijnBitPosition2[(uint32_t)(v * 0x077CB531U) >> 27]; which calculates n = log2 v where v is known to be a power of 2. In this case 0x077CB531 is an ordinary De Bruijn sequence, and the rest is obvious.

How to find multiplicative partitions of any integer?

北城以北 提交于 2019-11-28 08:27:35
I'm looking for an efficient algorithm for computing the multiplicative partitions for any given integer. For example, the number of such partitions for 12 is 4, which are 12 = 12 x 1 = 4 x 3 = 2 x 2 x 3 = 2 x 6 I've read the wikipedia article for this, but that doesn't really give me an algorithm for generating the partitions (it only talks about the number of such partitions, and to be honest, even that is not very clear to me!). The problem I'm looking at requires me to compute multiplicative partitions for very large numbers (> 1 billion), so I was trying to come up with a dynamic

F# parallelizing issue when calculating perfect numbers?

半世苍凉 提交于 2019-11-28 06:01:08
问题 I am trying to optimize a small program which calculates perfect numbers from a given exponent. The program runs (almost) perfectly, but when I open the task manager, it still runs on a single thread. That means that I must be doing something wrong, but my knowledge of F# is still in a 'beginning' phase. I will try to put this question as clear as I possibly can, but if I fail in doing so, please let me know. A perfect number is a number where the sum of all it's divisors (except for the

How to find the local minima of a smooth multidimensional array in NumPy efficiently?

回眸只為那壹抹淺笑 提交于 2019-11-28 05:33:04
Say I have an array in NumPy containing evaluations of a continuous differentiable function, and I want to find the local minima. There is no noise, so every point whose value is lower than the values of all its neighbors meets my criterion for a local minimum. I have the following list comprehension which works for a two-dimensional array, ignoring potential minima on the boundaries: import numpy as N def local_minima(array2d): local_minima = [ index for index in N.ndindex(array2d.shape) if index[0] > 0 if index[1] > 0 if index[0] < array2d.shape[0] - 1 if index[1] < array2d.shape[1] - 1 if

Finding where plots may cross with octave / matlab

亡梦爱人 提交于 2019-11-28 05:26:43
问题 I have several data points that are plotted below and I would like to find the frequency value when the amplitude value crosses 4 . I've included an example along with the data points in the example below. I've circled the answer graphically but I'm not sure how to compute it mathematically and get all the values for the frequencies I desire. How can I do this with octave / matlab? Also is there a mathematical term for what I'm trying to do? In this example I'm trying to get 5 frequencies

Code-golf: generate pascal's triangle

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:30:10
Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick ): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)] Explanation: the first element of the list comprehension (when the length is 0) is [1] the next elements are obtained the following way: take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end. e.g. for the 2nd step, we take [1] and make [0,1] and [1

How to find the exact set of operations for a specific number?

风流意气都作罢 提交于 2019-11-28 02:03:14
I am trying to implement a program that answers this problem : if you are given a specific number (for example : 268) and another 6 numbers (for example : 2,4,5,25,75,100) How can I find the operation that gives me the exact answer or the closest one to it? You can answer the previous example by using this operation : 75*4-25-5-2 = 268 Rules : you can use these arithmetic operations : +, -, *, / , (). when you use division the reminder must be equal to 0 (6/3 is ok but 6/4 is not ok!). you can not use the same number more than once. Also, you can avoid using a number (for example : 100 in our

Finding a Eulerian Tour

丶灬走出姿态 提交于 2019-11-28 00:40:39
问题 I am trying to solve a problem on Udacity described as follows: # Find Eulerian Tour # # Write a function that takes in a graph # represented as a list of tuples # and return a list of nodes that # you would follow on an Eulerian Tour # # For example, if the input graph was # [(1, 2), (2, 3), (3, 1)] # A possible Eulerian tour would be [1, 2, 3, 1] I came up with the following solution, which, while not as elegant as some of the recursive algorithms, does seem to work within my test case. def