I am having four set of values,namely
S(which ranges from x to y with a variation of .5),
C(which ranges from a to b with a variation of .25),
A(which ranges from p to q with a variation of 1)
Ad(which ranges from c to d with a variation of 1.5).
For each value of S, I should get all possible combinations of values from the other three sets.Can u help me please by suggesting suitable code........
Multiply all values with some constant so that you came into "integer problem domain". Then make 4 nested loops, for Si, Ci, Ai and Adi (S-integer, C-integer, ...). This way you will get all combinations. To get back to "float domain" divide with before mentioned constant.
EDIT:
Forget about previous suggestion. Try something like this:
double x = 1.1, y = 5.1, a = 6.1, b = 7.1, p = 8.1, q = 9.1, c = 10.1, d = 15.1;
double S, C, A, Ad;
for (S=x; S <= y; S = S + .5)
for (C=a; C <= b; C = C + .25)
for (A=p; A <= q; A = A + 1.0)
for (Ad=c; Ad <= d; Ad = Ad + 1.5)
Console.WriteLine("S={0} C={1} A={2}, Ad={3}", S, C, A, Ad);
Console.ReadLine();
Related
I was looking for a function to determine baseline of xy chart. I found some codes that was written in python.
Ex: Python baseline correction library
def baseline_als(y, lam, p, niter=10):
L = len(y)
D = sparse.csc_matrix(np.diff(np.eye(L), 2))
w = np.ones(L)
for i in xrange(niter):
W = sparse.spdiags(w, 0, L, L)
Z = W + lam * D.dot(D.transpose())
z = spsolve(Z, w*y)
w = p * (y > z) + (1-p) * (y < z)
return z
But I don't know how to convert it to C# the result of this code is used to remove the baseline like the following image
This code is based on "Asymmetric Least Squares Smoothing" algorithm by P. Eilers and H. Boelens
If you know any library or sample code for algorithms like "Asymmetric Least Squares Smoothing", please share with me
So I have a problem that I'm stuck on it since 3 days ago.
You want to participate at the lottery 6/49 with only one winning variant(simple) and you want to know what odds of winning you have:
-at category I (6 numbers)
-at category II (5 numbers)
-at category III (4 numbers)
Write a console app which gets from input the number of total balls, the number of extracted balls, and the category, then print the odds of winning with a precision of 10 decimals if you play with one simple variant.
Inputs:
40
5
II
Result I must print:
0.0002659542
static void Main(string[] args)
{
int numberOfBalls = Convert.ToInt32(Console.ReadLine());
int balls = Convert.ToInt32(Console.ReadLine());
string line = Console.ReadLine();
int theCategory = FindCategory(line);
double theResult = CalculateChance(numberOfBalls, balls, theCategory);
Console.WriteLine(theResult);
}
static int FindCategory (string input)
{
int category = 0;
switch (input)
{
case "I":
category = 1;
break;
case "II":
category = 2;
break;
case "III":
category = 3;
break;
default:
Console.WriteLine("Wrong category.");
break;
}
return category;
}
static int CalculateFactorial(int x)
{
int factorial = 1;
for (int i = 1; i <= x; i++)
factorial *= i;
return factorial;
}
static int CalculateCombinations(int x, int y)
{
int combinations = CalculateFactorial(x) / (CalculateFactorial(y) * CalculateFactorial(x - y));
return combinations;
}
static double CalculateChance(int a, int b, int c)
{
double result = c / CalculateCombinations(a, b);
return result;
}
Now my problems: I'm pretty sure I have to use Combinations. For using combinations I need to use Factorials. But on the combinations formula I'm working with pretty big factorials so my numbers get truncated. And my second problem is that I don't really understand what I have to do with those categories, and I'm pretty sure I'm doing wrong on that method also. I'm new to programming so please bare with me. And I can use for this problem just basic stuff, like conditions, methods, primitives, arrays.
Let's start from combinatorics; first, come to terms:
a - all possible numbers (40 in your test case)
t - all taken numbers (5 in your test case)
c - category (2) in your test case
So we have
t - c + 1 for numbers which win and c - 1 for numbers which lose. Let's count combinations:
All combinations: take t from a possible ones:
A = a! / t! / (a - t)!
Winning numbers' combinations: take t - c + 1 winning number from t possible ones:
W = t! / (t - c + 1)! / (t - t + c - 1) = t! / (t - c + 1)! / (c - 1)!
Lost numbers' combinations: take c - 1 losing numbers from a - t possible ones:
L = (a - t)! / (c - 1)! / (a - t - c + 1)!
All combinations with category c, i.e. with exactly t - c + 1 winning and c - 1 losing numbers:
C = L * W
Probability:
P = C / A = L * W / A =
t! * t! (a - t)! * (a - t)! / (t - c + 1)! / (c - 1)! / (c - 1)! / (a - t- c + 1)! / a!
Ugh! Not let's implement some code for it:
Code:
// double : note, that int is too small for 40! and the like values
private static double Factorial(int value) {
double result = 1.0;
for (int i = 2; i <= value; ++i)
result *= i;
return result;
}
private static double Chances(int a, int t, int c) =>
Factorial(a - t) * Factorial(t) * Factorial(a - t) * Factorial(t) /
Factorial(t - c + 1) /
Factorial(c - 1) /
Factorial(c - 1) /
Factorial(a - t - c + 1) /
Factorial(a);
Test:
Console.Write(Chances(40, 5, 2));
Outcome:
0.00026595421332263435
Edit:
in terms of Combinations, if C(x, y) which means "take y items from x" we
have
A = C(a, t); W = C(t, t - c + 1); L = C(a - t, c - 1)
and
P = W * L / A = C(t, t - c + 1) * C(a - t, c - 1) / C(a, t)
Code for Combinations is quite easy; the only trick is that we return double:
// Let'g get rid of noisy "Compute": what else can we do but compute?
// Just "Combinations" without pesky prefix.
static double Combinations(int x, int y) =>
Factorial(x) / Factorial(y) / Factorial(x - y);
private static double Chances(int a, int t, int c) =>
Combinations(t, t - c + 1) *
Combinations(a - t, c - 1) /
Combinations(a, t);
You can fiddle the solution
I am trying to create a small software that does the Affine Cipher, which means that K1 and the amount of letters in the alphabet (using m for this number) must be coprime, that is gcd(k1, m) == 1.
Basically it's like this:
I have a plaintext: hey
I have K1: 7
I have K2: 5
Plaintext in numerical format is:
8 5 25
8 - from h (the position in the alphabet) and **
5 25** goes the same for e and y
Encrypted: 7 13 18
Which is the formula:
k1 * 8 + k2 mod 27 = 7
k1 * 5 + k2 mod 27 = 13
k1 * 25 + k2 mod 27 = 18
I have a function that crypts this but I don't know how to decrypt.
For example I have 7 for h. I want to get the number 8 back again, knowing 7, k1 and k2.
Do you guys have any ideas ?
Some function where you input k1, k2, result (7 for example, for h), and it gives me back 8, but I really don't know how to reverse this.
The function for encryption is this:
public List<int> get_crypted_char(string[] strr)
{
List<int> l = new List<int>();
int i;
for (i = 0; i < strr.Length; i++)
{
int ch = int.Parse(strr[i]);
int numberback = k1 * ch + 5;
numberback = (numberback % 27);
l.Add(numberback);
}
return l;
}
Where: string[] strr is a string that contains the plaintext.
Function example:
get_crypted_char({"e","c","b"})
The result would be a list like this {"5","3","2"}
UPDATE:
Here is a link from wikipedia about this encryption, and also decryption, but ... I don't really understand "how to"
http://en.wikipedia.org/wiki/Affine_cipher
It is not possible (in general case, for affine cipher, see update below). That's why module operation is so frequently used in security algorithms - it is not reversible. But, why don't we try?
result = (k1 * input + k2) % 27 (*1)
Let's take the first letter:
result = (7 * 8 + 5) % 27 = 7
That's cool. Now, because we said, that:
result = (k1 * input + k2) % 27
the following is also true:
k1 * input + k2 = 27 * div + result (*2)
where
div = (k1 * input + k2) / 27 (integral division)
It is quite obvious (if a % b = c, then a = b*n + c, where n is the result of integer division a/b).
You know the values of k1 (which is 7), k2 (5) and result (7). So, when we put these values to (*2), we get the following:
7 * input + 5 = 27 * div + 7 //You need to solve this
As you can see, it is impossible to solve this, because you would need to know also the result of the integral division - translating this to your function's language, you would need the value of
numberback / 27
which is unknown. So answer is: you cannot reverse your function's results, using only output it returns.
** UPDATE **
I focused too much on the question's title, so the answer above is not fully correct. I decided not to remove it, however, but write an update.
So, the answer for your particular case (affine cipher) is: YES, you can reverse it.
As you can see on the wiki, decryption function for affine cipher for the following encrytption function:
E(input) = a*input + b mod m
is defined as:
D(enc) = a^-1 * (enc - b) mod m
The only possible problem here can be computation of a^-1, which is modular multiplicative inverse.
Read about it on wiki, I will provide only example.
In your case a = k1 = 7 and m = 27. So:
7^-1 = p mod 27
7p = 1 mod 27
In other words, you need to find p, which satisfies the following: 7p % 27 = 1.
p can be computed using extended euclidean algorithm and I computed it to be 4 (4 * 7 = 28, 28 % 27 = 1).
Check, if can decipher your output now:
E(8) = 7*8 + 5 mod 27 = 7
D(7) = 4 * (7 - 5) mod 27 = 8
Hope that helps :)
Please note that the other answers do not take into account the the algorithm at hand is the Affine Cipher, ie there are some conditions at hand, the most important one the coprime status of k1 and m.
In your case it would be:
m = 27; // letters in your alphabet
k1 = 7; // coprime with m
k2 = 5; // no reqs here, just that a value above 27 is the same as mod 27 of that value
int Encrypt(int letter) {
return ((letter * k1) + k2) % m;
}
int Decrypt(int letter) {
return ((letter - k2) * modInverse(k1, m)) % m;
}
Tuple<int, Tuple<int, int>> extendedEuclid(int a, int b)
{
int x = 1, y = 0;
int xLast = 0, yLast = 1;
int q, r, m, n;
while (a != 0)
{
q = b / a;
r = b % a;
m = xLast - q * x;
n = yLast - q * y;
xLast = x; yLast = y;
x = m; y = n;
b = a; a = r;
}
return new Tuple<int, Tuple<int, int>>(b, new Tuple<int, int>(xLast, yLast));
}
int modInverse(int a, int m)
{
return (extendedEuclid(a, m).Item2.Item1 + m) % m;
}
ModInverse implementation taken from http://comeoncodeon.wordpress.com/2011/10/09/modular-multiplicative-inverse/.
I have created a program that will tell the modular inverse of something. I will let you use it. It is posted below.
# Cryptomath Module
def gcf(a, b):
# Return the GCD of a & b using Euclid's Algorithm
while a != 0:
a, b = b % a, a
return b
def findModInverse(a, m):
# Return the modular inverse of a % m, which is
# the number x such that a*x % m = 1
if gcf(a, m) != 1:
return None # No mode inverese if a & m aren't relatively prime
# Calculate using the Extended Euclidean Algorithm:
u1, u2, u3 = 1, 0, a
v1, v2, v3 = 0, 1, m
while v3 != 0:
q = u3 // v3 # // is the integer division operator
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q *
v3), v1, v2, v3
return u1 % m
Note: The modular inverse is found using the extended euclidean algorithm. Here is the Wikipedia entry for it: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm.
Note: This needs to be imported as a module to be used. Hope it helps.
I'm trying to implement an algorithm to Howellize a matrix, in the way explained on page 5 of this paper (google docs link) (link to the pdf).
Most of it is pretty obvious to me, I think, but I'm not sure about line 16, does >> mean a right shift there? If it does, then how does it even work? Surely it would mean that bits are being chopped off? As far as I know there's no guarantee at that point that the number it is shifting is being shifted by an amount that preserves the information.
And if it doesn't mean a right shift, what does it mean?
If anyone can spare the time, I'd also like to have a test case (I don't trust myself to come up with one, I don't understand it well enough).
I've implemented it like this, is that correct? (I don't have a test case, so how can I find out?)
int j = 0;
for (int i = 0; i < 2 * k + 1; i++)
{
var R = (from row in rows
where leading_index(row) == i
orderby rank(row[i]) ascending
select row).ToList();
if (R.Count > 0)
{
uint[] r = R[0];
int p = rank(r[i]); // rank counts the trailing zeroes
uint u = r[i] >> p;
invert(r, u); // multiplies each element of r by the
// multiplicative inverse of u
for (int s = 1; s < R.Count; s++)
{
int t = rank(R[s][i]);
uint v = R[s][i] >> t;
if (subtract(R[s], r, v << (t - p)) == 0)
// subtracts (v<<(t-p)) * r from R[s],
// removes if all elements are zero
rows.Remove(R[s]);
}
swap(rows, rows.IndexOf(r), j);
for (int h = 0; h < j - 1; h++)
{
uint d = rows[h][i] >> p;
subtract(rows[h], r, d);
}
if (r[i] != 1)
// shifted returns r left-shifted by 32-p
rows.Add(shifted(r, 32 - p));
j++;
}
}
For test case, this may help you (page no #2). Also try this.
I think that you are right about the right shift. To get the Howell form, they want the values other than leading value in a column to be smaller than the leading value. Right shifting seems fruitful for that.
line 16 says:
Pick d so that 0 <= G(h,i) - d * ri < ri
Consider
G(h,i) - d * ri = 0
G(h,i) = d * ri
G(h,i) = d * (2 ^ p) ... as the comment on line 8 says, ri = 2^p.
So d = G(h,i) / (2 ^ p)
Right shifting G(h,i) by p positions is the quickest way to compute the value of d.
I asked a question about having the Excel's BetaInv function ported to .NET: BetaInv function in SQL Server
now I managed to write that function in pure dependency less C# code and I do get the same results than in MS Excel up to 6 or 7 digits after comma, results are fine for us, the problem is that such code is embedded in a SQL CLR Function and gets called thousands of time from a stored procedure and makes the execution of the whole procedure about 50% slower, from 30 seconds up to a minute if I use that function or not.
here some code of it, I am not asking a deep analysis but is there anybody who sees any major performance issue in the way I am doing this calculations? like for example usage of other data types instead of doubles or whatsoever... ?
private static double betacf(double a, double b, double x)
{
int m, m2;
double aa, c, d, del, h, qab, qam, qap;
qab = a + b;
qap = a + 1.0;
qam = a - 1.0;
c = 1.0; // First step of Lentz’s method.
d = 1.0 - qab * x / qap;
if (System.Math.Abs(d) < FPMIN)
{
d = FPMIN;
}
d = 1.0 / d;
h = d;
for (m = 1; m <= MAXIT; ++m)
{
m2 = 2 * m;
aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1.0 + aa * d; //One step (the even one) of the recurrence.
if (System.Math.Abs(d) < FPMIN)
{
d = FPMIN;
}
c = 1.0 + aa / c;
if (System.Math.Abs(c) < FPMIN)
{
c = FPMIN;
}
d = 1.0 / d;
h *= d * c;
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
d = 1.0 + aa * d; // Next step of the recurrence (the odd one).
if (System.Math.Abs(d) < FPMIN)
{
d = FPMIN;
}
c = 1.0 + aa / c;
if (System.Math.Abs(c) < FPMIN)
{
c = FPMIN;
}
d = 1.0 / d;
del = d * c;
h *= del;
if (System.Math.Abs(del - 1.0) < EPS)
{
// Are we done?
break;
}
}
if (m > MAXIT)
{
return 0;
}
else
{
return h;
}
}
private static double gammln(double xx)
{
double x, y, tmp, ser;
double[] cof = new double[] { 76.180091729471457, -86.505320329416776, 24.014098240830911, -1.231739572450155, 0.001208650973866179, -0.000005395239384953 };
y = xx;
x = xx;
tmp = x + 5.5;
tmp -= (x + 0.5) * System.Math.Log(tmp);
ser = 1.0000000001900149;
for (int j = 0; j <= 5; ++j)
{
y += 1;
ser += cof[j] / y;
}
return -tmp + System.Math.Log(2.5066282746310007 * ser / x);
}
The only thing that stands out for me, and is usually a performance hit, is memory allocation. I don't know how often gammln is called but you might want to move the double[] cof = new double[] {} to a static one time only allocation.
double is usually the best type. Especially since the functions in Math take doubles. Unfortunately I see no obvious improvements to make on your code.
It might be possible to use look up tables to get a better first estimate on which you iterate, but since I don't know the Math behind what you're doing I don't know if that's possible in this specific case.
Obviously larger epsilons will improve performance. So choose it as large as possible while fulfilling your accuracy demands.
If the function gets called repeatedly with the same parameters you might be able to cache results.
One thing that looks odd is the way you force small values for c, d,... to FPMIN. My instinct is that this might lead to suboptimal step sizes.
All I've got is unrolling the j loop in gammln, but it'll make at most a tiny difference.
A more radical thought would be to rewrite in pure T-SQL, since it has everything you use: + - * / abs log are all available.