math

Making sure unsigned int/long always execute in checked context in C#

有些话、适合烂在心里 提交于 2020-01-24 08:49:10
问题 Has anyone found it strange that the default context for uint and ulong is unchecked rather than checked considering that they are meant to represent values that can never be negative? So if some code is trying to violate that constraint it seems to me the natural and preferred behaviour would be to throw an exception rather than returning the max value instead (which can easily leave important pieces of data in an invalid state and impossible to revert..). Is there an existing attribute

Solving the recurrence T(n) = 2T(sqrt(n))

别来无恙 提交于 2020-01-24 06:00:23
问题 I would like to solve the following recurrence relation: T(n) = 2T(√n); I'm guessing that T(n) = O(log log n) , but I'm not sure how to prove this. How would I show that this recurrence solves to O(log log n) ? 回答1: One idea would be to simplify the recurrence by introducing a new variable k such that 2 k = n. Then, the recurrence relation works out to T(2 k ) = 2T(2 k/2 ) If you then let S(k) = T(2 k ), you get the recurrence S(k) = 2S(k / 2) Note that this is equivalent to S(k) = 2S(k / 2)

Python and numpy : subtracting line by line a 2-dim array from a 1-dim array

杀马特。学长 韩版系。学妹 提交于 2020-01-24 05:37:45
问题 In python, I wish to subtract line by line a 2-dim array from a 1-dim array. I know how to do it with a 'for' loop and indexes but I suppose it may be quicker to use numpy functions. However I did not find a way to do it. Here is an example with a 'for' loop : from numpy import * x=array([[1,2,3,4,5],[6,7,8,9,10]]) y=array([20,10]) j=array([0, 1]) a=zeros([2,5]) for i in j : ... a[i]=y[i]-x[i] And here is an example of something that does not work, replacing the 'for' loop by this: a=y[j]-x[j

Detect overlapping of rectangular prisms

空扰寡人 提交于 2020-01-24 04:56:25
问题 Given a 3D coordinate system and rectangular prisms with a non-negative starting point and a non-negative size (e.g. starts at (0, 2, 5) and has a size of (9, 20, 5) ): how can I best check if another rectangular prism intersects with one of the prisms already in the coordinate system? Eventually, the goal would be to perform this check for all prisms present, being able to test one should be sufficient to complete this task. Info: starting points and sizes are 3-tuples of non-negative longs.

Calculate coordinates of a point with given distances to two other points

匆匆过客 提交于 2020-01-23 18:37:29
问题 If I have three points A, B, C and I know the distances between them and A is at 2D coordinates {0,0} and B is at {ab,0}, then what would be the formula to find the coordinates of the point C? 回答1: The point {cx, cy} has to solve two equations: cx^2+cy^2==ac^2 && (cx-ab)^2+cy^2==bc^2 => cx^2-(cx-ab)^2==ac^2-bc^2 => 2*cx*ab==ac^2-bc^2+ab^2 => cx = (ac^2-bc^2+ab^2)/(2*ab) => cy = +/- sqrt(ac^2-cx^2) iff ac^2-cx^2 > 0 => cy = 0 iff ac^2-cx^2 = 0 => no solution else There are either two points

Implementing Exponential Moving Average in C++

无人久伴 提交于 2020-01-23 12:33:06
问题 I am developing a small trading robot as an exercise. He receives stock prices day after day (represented as iterations). Here's what my Trade class looks like: class Trade { private: int capital_; int days_; // Total number of days of available stock prices int daysInTrading_; // Increments as days go by. std::list<int> stockPrices_; // Contains stock prices day after day. int currentStock_; // Current stock we are dealing with. int lastStock_; // Last stock dealt with int trend_; // Either

Implementing Exponential Moving Average in C++

自闭症网瘾萝莉.ら 提交于 2020-01-23 12:32:27
问题 I am developing a small trading robot as an exercise. He receives stock prices day after day (represented as iterations). Here's what my Trade class looks like: class Trade { private: int capital_; int days_; // Total number of days of available stock prices int daysInTrading_; // Increments as days go by. std::list<int> stockPrices_; // Contains stock prices day after day. int currentStock_; // Current stock we are dealing with. int lastStock_; // Last stock dealt with int trend_; // Either

strange results when converting floating point numbers to fractions using MASS::fractions in R

浪尽此生 提交于 2020-01-23 12:07:16
问题 I am trying to do something similar to what is discussed in this post, but in R rather than Python. However: require(MASS) fractions(0.723618,max.denominator = 1000000) #[1] 89/123 This seems to indicate that floating point number 0.723618 is better described by fraction 89/123 than by 361809/500000, which does not seem correct to me. Even more puzzling: fractions(0.7236,max.denominator = 100000000000) #[1] 89/123 Surely it would be better to write 0.7236 as 1809/5000, wouldn't it? Do you

Square root of complex numbers in python

早过忘川 提交于 2020-01-23 04:30:06
问题 I have run across some confusing behaviour with square roots of complex numbers in python. Running this code: from cmath import sqrt a = 0.2 b = 0.2 + 0j print(sqrt(a / (a - 1))) print(sqrt(b / (b - 1))) gives the output 0.5j -0.5j A similar thing happens with print(sqrt(-1 * b)) print(sqrt(-b)) It appears these pairs of statements should give the same answer? 回答1: Both answers ( +0.5j and -0.5j ) are correct, since they are complex conjugates -- i.e. the real part is identical, and the

Return elements of the Groebner Basis as they are found

安稳与你 提交于 2020-01-22 19:17:11
问题 This question could refer to any computer algebra system which has the ability to compute the Groebner Basis from a set of polynomials (Mathematica, Singular, GAP, Macaulay2, MatLab, etc.). I am working with an overdetermined system of polynomials for which the full groebner basis is too difficult to compute, however it would be valuable for me to be able to print out the groebner basis elements as they are found so that I may know if a particular polynomial is in the groebner basis. Is there