math

Looking for a .NET Function that sums up number and instead of overflowing simply returns int.MaxValue

I use int.MaxValue as a penalty and sometimes I am computing the penalties together. Is there a function or how would you create one with the most grace and efficiency that does that. ie. 50 + 100 = 150 int.Max + 50 = int.Max and not int.Min + 50 ...

What is the best method to convert to an Integer in JavaScript?

There are several different methods for converting floating point numbers to Integers in JavaScript. My question is what method gives the best performance, is most compatible, or is considered the best practice? Here are a few methods that I know of: var a = 2.5; window.parseInt(a); // 2 Math.floor(a); // 2 a | 0; // ...

Performing math functions within ant ReplaceRegExp

I need to increment a number in a source file from an ant build script. I can use the ReplaceRegExp task to find the number I want to increment, but how do I then increment that number within the replace attribute? Heres what I've got so far: <replaceregexp file="${basedir}/src/path/to/MyFile.java" match="MY_PROPERTY = ([0-9]{1,});...

Is there a library for Linear algebra matrix handling in Java?

Is there any libraries in java that allow using mathematical matrices? I am looking for a library that allows me to perform operations in matrices such as invert, scalar multiplication, linear transformations, etc. etc. etc. In a nutshell, the operations required for lineal algebra. ...

Is there a built in method for converting radians to degrees?

I run into this occasionally and always forget how to do it. One of those things that pop up ever so often. Also, what's the formula to convert angles expressed in radians to degrees and back again? ...

Probability problem - Duplicates when choosing from large basket.

I need to explain to the client why dupes are showing up between 2 supposedly different exams. It's been 20 years since Prob and Stats. I have a generated Multiple choice exam. There are 192 questions in the database, 100 are chosen at random (no dupes). Obviously, there is a 100% chance of there being at least 8 dupes between any two...

Is programming a subset of math?

I've heard many times that all programming is really a subset of math. Some suggest that OO, at its roots, is mathematically based, but I don't get the connection, aside from some obvious examples: using induction to prove a recursive algorithm, formal correctness proofs, functional languages, lambda calculus, asymptotic complexity, DF...

Recommend an Open Source .NET Statistics Library

I need to calculate averages, standard deviations, medians etc for a bunch of numerical data. Is there a good open source .NET library I can use? I have found NMath but it is not free and may be overkill for my needs. ...

How can a transform a polynomial to another coordinate system?

Using assorted matrix math, I've solved a system of equations resulting in coefficients for a polynomial of degree 'n' Ax^(n-1) + Bx^(n-2) + ... + Z I then evaulate the polynomial over a given x range, essentially I'm rendering the polynomial curve. Now here's the catch. I've done this work in one coordinate system we'll call "data sp...

What do you use for fixed point representation in C++?

I'm looking for a fixed-point standard to use for financial data, do you know any that is worth trying? Do you have any experience on the performance of that hand-made fixed-point classes? ...

Least common multiple for 3 or more numbers

How do you calculate the least common multiple of multiple numbers? So far I've only been able to calculate it between two numbers. But have no idea how to expand it to calculate 3 or more numbers. So far this is how I did it LCM = num1 * num2 / gcd ( num1 , num2 ) With gcd is the function to calculate the greatest common divisor...

Mathematics for Computer Science Students

To cut a long story short, I am a CS student that has received no formal Post-16 Maths education for years. Right now even my Algebra is extremely rusty and I have a couple of months to shape up my skills. I've got a couple of video lectures in my bookmarks, consisting of: Pre-Calculus Algebra Calculus Probability Introduction to Stati...

Gaussian distributions with PHP on a 24h time period

How can I set points on a 24h period spreaded by the Gaussian distributions? For example to have the peak at 10 o'clock? ...

Calculate the position of an accelerating body after a certain time

How do I calculate the position of an accelerating body (e.g. a car) after a certain time (e.g. 1 second)? For a moving body that it not accelerating, it is a linear relationship, so I presume for an accelerating body it involves a square somewhere. Any ideas? ...

Invert 4x4 matrix - Numerical most stable solution needed.

I want to invert a 4x4 matrix. My numbers are stored in fixed-point format (1.15.16 to be exact). With floating-point arithmetic I usually just build the adjoint matrix and divide by the determinant (e.g. brute force the solution). That worked for me so far, but when dealing with fixed point numbers I get an unacceptable precision loss ...

Is mathematics necessary for programming?

Hello all, I happened to debate with a friend during college days whether advanced mathematics is necessary for any veteran programmer. He used to argue fiercely against that. He said that programmers need only basic mathematical knowledge from high school or fresh year college math, no more no less, and that almost all of programming t...

Visiting the points in a triangle in a random order..

For a right triangle specified by an equation aX + bY <= c on integers I want to plot each pixel(*) in the triangle once and only once, in a pseudo-random order, and without storing a list of previously hit points. I know how to do this with a line segment between 0 and x pick a random point'o' along the line, pick 'p' that ...

Iterating shuffled [0..n) without arrays

I know of a couple of routines that work as follows: Xn+1 = Routine(Xn, max) For example, something like a LCG generator: Xn+1 = (a*Xn + c) mod m There isn't enough parameterization in this generator to generate every sequence. Dream Function: Xn+1 = Routine(Xn, max, permutation number) This routine, para...

How are exponents calculated?

I'm trying to determine the asymptotic run-time of one of my algorithms, which uses exponents, but I'm not sure of how exponents are calculated programmatically. I'm specifically looking for the pow() algorithm used for double-precision, floating point numbers. ...

What is the best way to get all the divisors of a number?

Here's the very dumb way: def divisorGenerator(n): for i in xrange(1,n/2+1): if n%i == 0: yield i yield n The result I'd like to get is similar to this one, but I'd like a smarter algorithm (this one it's too much slow and dumb :-) I can find prime factors and their multiplicity fast enough. I've an generator that ge...