discrete-mathematics

Compute a derivative using discrete methods

倖福魔咒の 提交于 2019-11-27 20:49:37
I am looking for a method to compute a derivative using a discrete and fast method. Since now I do not know the type of equation I have, I am looking for discrete methods analog to the ones that we can find for the integral such as, the Euler method. Andrea Ambu I think you are looking for the derivative calculated in a point. If this is the case, here there is a simple way to do that. You need to know the derivative in a point, say a . It is given by the limit of the difference quotient for h->0: You actually need to implement the limit function. So you: Define an epsilon, set it more small

Find non-common elements in lists

▼魔方 西西 提交于 2019-11-27 20:29:06
I'm trying to write a piece of code that can automatically factor an expression. For example, if I have two lists [1,2,3,4] and [2,3,5], the code should be able to find the common elements in the two lists, [2,3], and combine the rest of the elements together in a new list, being [1,4,5]. From this post: How to find list intersection? I see that the common elements can be found by set([1,2,3,4]&set([2,3,5]). Is there an easy way to retrieve non-common elements from each list, in my example being [1,4] and [5]? I can go ahead and do a for loop: lists = [[1,2,3,4],[2,3,5]] conCommon = [] common

How do you calculate log base 2 in Java for integers?

孤街浪徒 提交于 2019-11-27 16:45:38
I use the following function to calculate log base 2 for integers: public static int log2(int n){ if(n <= 0) throw new IllegalArgumentException(); return 31 - Integer.numberOfLeadingZeros(n); } Does it have optimal performance? Does someone know ready J2SE API function for that purpose? UPD1 Surprisingly for me, float point arithmetics appears to be faster than integer arithmetics. UPD2 Due to comments I will conduct more detailed investigation. UPD3 My integer arithmetic function is 10 times faster than Math.log(n)/Math.log(2). If you are thinking about using floating-point to help with

Which algorithm for assigning shifts (discrete optimization problem)

对着背影说爱祢 提交于 2019-11-27 14:46:48
问题 I'm developing an application that optimally assigns shifts to nurses in a hospital. I believe this is a linear programming problem with discrete variables, and therefore probably NP-hard: For each day, each nurse (ca. 15-20) is assigned a shift There is a small number (ca. 6) of different shifts There is a considerable number of constraints and optimization criteria, either concerning a day, or concerning an emplyoee, e.g.: There must be a minimum number of people assigned to each shift

Height of a tree with only one node

馋奶兔 提交于 2019-11-27 13:04:54
问题 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)? 回答1: 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

PHP take all combinations

谁说胖子不能爱 提交于 2019-11-27 08:58:01
I saw this algorithm that will take numbers or words and find all possible combinations And I'm using it, but it does NOT return all "real" combinations. PHP: <?php require_once 'Math/Combinatorics.php'; $words = array('cat', 'dog', 'fish'); $combinatorics = new Math_Combinatorics; foreach($combinatorics->permutations($words, 2) as $p) { echo join(' ', $p), "\n"; } ?> And it returns: cat dog dog cat cat fish fish cat dog fish fish dog But these are not all real combinations, all real combinations includes these too: cat cat dog dog fish fish And that is what I need, the method to get all real

Code-golf: generate pascal's triangle

你说的曾经没有我的故事 提交于 2019-11-27 05:21:41
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. 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

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

一世执手 提交于 2019-11-27 00:58:13
问题 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

Compute a derivative using discrete methods

余生长醉 提交于 2019-11-26 22:58:48
问题 I am looking for a method to compute a derivative using a discrete and fast method. Since now I do not know the type of equation I have, I am looking for discrete methods analog to the ones that we can find for the integral such as, the Euler method. 回答1: I think you are looking for the derivative calculated in a point. If this is the case, here there is a simple way to do that. You need to know the derivative in a point, say a . It is given by the limit of the difference quotient for h->0:

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

随声附和 提交于 2019-11-26 22:05:41
问题 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