math

How do you normalize a zero vector

Suppose you have a function 'normalize' which takes a list of numbers (representing a vector) as input and returns the normalized vector. What should the result be when the vector is all zeros or the sum of its components is zero? ...

When is it appropriate to use floating precision data types?

It's clear that one shouldn't use floating precision when working with, say, monetary amounts since the variation in precision leads to inaccuracies when doing calculations with that amount. That said, what are use cases when that is acceptable? And, what are the general principles one should have in mind when deciding? ...

Does an open-ended interval implementation exist for Java?

I've got a classification of certain values in different intervals. Most have the form [20-30], but some are of the form [30-infinite). Is the an interval class you know of which can represent: an interval which is not closed on both sides (e.g. (0-5) or [0-5) ) an interval which closes (or starts) on infinite ...

Print two-dimensional array in spiral order

How do I print a 5×5 two-dimensional array in spiral order? Is there any formula so that I can print an array of any size in spiral order? ...

Finding all the subsets of a set

I need an algorithm to find all of the subsets of a set where the number of elements in a set is n. S={1,2,3,4...n} Edit: I am having trouble understanding the answers provided so far. I would like to have step-by-step examples of how the answers work to find the subsets. For example, S={1,2,3,4,5} How do you know {1} and {1,2} ar...

Optimal Layout Algorithm

I'm implementing a CSS Sprites setup in my ASP.NET application based on this article.. http://weblogs.asp.net/zowens/archive/2008/03/05/css-sprite-for-asp-net.aspx In the section "Get the SPRITE already!"... the author says... "The only real down side to this is that all your images are lined up vertically. I'm not quite sure that thi...

Interview question: f(f(n)) == -n

A question I got on my last interview: Design a function f, such that: f(f(n)) == -n Where n is a 32 bit signed integer; you can't use complex numbers arithmetic. If you can't design such a function for the whole range of numbers, design it for the largest range possible. Any ideas? ...

Interview question: f(f(x)) == 1/x

Design a function f such that: f(f(x)) == 1/x Where x is a 32 bit float Or how about Given a function f, find a function g such that f(x) == g(g(x)) See Also Interview question: f(f(n)) == -n ...

What's the first double that deviates from its corresponding long by delta?

I want to know the first double from 0d upwards that deviates by the long of the "same value" by some delta, say 1e-8. I'm failing here though. I'm trying to do this in C although I usually use managed languages, just in case. Please help. #include <stdio.h> #include <limits.h> #define DELTA 1e-8 int main() { double d = 0; // c...

Looking for an algorithm to spit out a sequence of numbers in a (pseudo) random order

Suppose I have a sequence of numbers: {n, n+1, n+2, ... n + m} Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3,...m} will spit out the original set in a random (or at least pseudo random) order. For example assume my sequence is {10, 11, 12, 13, 14, 15, 16, 17} f(1) could ...

Geometrical Arc to Bezier Curve

When drawing an Arc in 2D, using a Bezier Curve approximation, how does one calculate the two control points given that you have a center point of a circle, a start and end angle and a radius? ...

VIM: Bijection between VIM Taglist elements and code snippets?

My Taglist in a C code: macro || MIN_LEN || MAX_ITERATIONS ||- typedef || cell || source_cell ||- variable || len_given Taglist elements (domain): A = {MIN_LEN, MAX_ITERATIONS, cell, source_cell, len_given} Code snippets (codomain): B = {"code_MIN_LEN", "code_MAX_ITERATIONS", ..., "code_len_...

Hashtable/Dictionary collisions

Using the standard English letters and underscore only, how many characters can be used at a maximum without causing a potential collision in a hashtable/dictionary. So strings like: blur Blur b Blur_The_Shades_Slightly_With_A_Tint_Of_Blue ... ...

How do you calculate the total number of all possible unique subsets from a set with repeats?

Given a set** S containing duplicate elements, how can one determine the total number all the possible subsets of S, where each subset is unique. For example, say S = {A, B, B} and let K be the set of all subsets, then K = {{}, {A}, {B}, {A, B}, {B, B}, {A, B, B}} and therefore |K| = 6. Another example would be if S = {A, A, B, B}, the...

Is there a CEIL version of MySQL's DIV operator?

With MySQL a DIV b is much faster than FLOOR(a / b). But I need to round up so I'm using, CEIL(a / b) It seems strange that there wouldn't be a ceiling version of DIV, but I can't find it. Is there anything undocumented hidden away somewhere? Or any other non floating point way to do this? ...

Random behavior differences between C# and Java: the seed

I have a Random into a class which aims to generate random sequences in different contexts: this is the result of a porting from Java code. In the Java version everything works fine since the java.lang.Random class has the method setSeed, which permits the change of the seed value dynamically. Random rnd = new Random(); ... rnd.nextInt(...

Is a given set of group elements a set of coset representatives?

I am afraid the question is a bit technical, but I hope someone might have stumbled into a similar subject, or give me a pointer of some kind. If G is a group (in the sense of algebraic structure), and if g1, ..., gn are elements of G, is there an algorithm (or a function in some dedicated program, like GAP) to determine whether there i...

Understanding Discrete Fourier Tranform

Hello, I have a small query regarding the discrete Fourier transforms. If I understand correctly, then what we do is convert a polynomial to its point value representation, with n points for a polynomial that goes up to the power of n-1. But why must we evaluate it at the nth roots of unity? Wouldn't any other n points uniquely identify ...

Best library for statistics in C++?

I'm looking for high performance code (needs to run in real-time), preferably open source, but if there is nothing that's free and high-perf, I'll take something well supported and of high quality for a cost. Any suggestions? These are the ones I've found so far, in no particular order: CodeCogs GSL Cephes Boost MathToolkit Blitz+...

How to calculate an RGB colour by specifying an alpha blending amount?

I'm writing a colour picker that gets the pixel RGB values from wherever you point to on the screen. I want to also have the option of specifying that the colour I picked already has an alpha value. I'm just wondering how I can calculate the resulting colour. For example: The resulting pixel colour is 240,247,249 but I know that the ...