Format Fraction on picker to lowest common denominator? - iphone
Below is how I am formatting my picker currently. I would really like it to show 1/8 instead of 2/16 or 1/2 instead of 8/16. How can I adjust this to show my desired output? Thank you!
fractionArray = [[NSMutableArray alloc] init];
for(int frac = 0; frac <= 15; frac ++){
NSString *fracString = [NSString stringWithFormat:#"%d/16", frac];
[fractionArray addObject:fracString]; // Add the string.
Kids these days... Euclidean algorithm... what are they teaching in school... grumble grumble...
int gcd(int a, int b) {
// assumes a >= 0 && b > 0
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
NSString *stringByReducingFraction(int a, int b) {
if (a == 0) return #"0";
if (a == b) return #"1";
int g = gcd(a, b);
return [NSString stringWithFormat:#"%d/%d", a / g, b / g];
}
This should work for any power of 2 denominator:
// dodge this special case:
[fractionArray addObject:#"0"];
for ( int numerator = 1; numerator <= 15; numerator++ )
{
int denominator = 16;
int num = numerator;
while ( num % 2 == 0 )
{
num /= 2;
denominator /= 2;
}
NSString *fracString = [NSString stringWithFormat:#"%d/%d", num, denominator];
[fractionArray addObject:fracString]; // Add the string.
}
And it's easy to extend this to any denominator. (Hint: replace 2 with n, iterate n from 2 up to sqrt(denominator).)
EDIT: actually works now!
Since I went ahead and coded it, here's the version that factors any denominator:
int denominator = 240;
for ( int numerator = 1; numerator < denominator; numerator++ )
{
int denom = denominator;
int num = numerator;
int factor = 2;
while ( factor * factor < denom )
{
while ( (num % factor) == 0 && (denom % factor) == 0 )
{
num /= factor;
denom /= factor;
}
// don't worry about finding the next prime,
// the loop above will skip composites
++factor;
}
NSString *fracString = [NSString stringWithFormat:#"%d/%d", num, denom];
[fractionArray addObject:fracString];
}
Related
Same C code different results TIv5.2.5 and gcc 5.4.1 c99 compiler
I am using MSP432P401R to do FFT of SAR ADC samples, did FFT in MATLAB and got results same as C compiler online but Code Composer Studio IDE is giving different output than MATLAB results, I thought that can be a compiler issue so tried reading same did some changes and tried but not getting results Like MATLAB. Online C compiler was gcc 5.4.1 c99. and in CCS TI v5.2.5 compiler is used. float m; float ur, ui, sr, si,tr, ti; long double Temp_A[256],ArrayA[256]={2676,2840,2838,2832,2826,2818,2814,2808, 2804,2798,2790,2784,2778,2770,2764,2758,2752,2746,2740,2734, 2726,2720,2714,2706,2700,2692,2686,2680,2674,2668,2660,2654, 2646,2642,2634,2624,2618,2612,2604,2598,2590,2584,2576,2570, 2562,2556,2550,2542,2536,2530,2522,2512,2508,2498,2490,2484, 2478,2470,2462,2454,2448,2442,2432,2426,2420,2414,2404,2398, 2390,2382,2374,2368,2360,2352,2346,2338,2330,2322,2314,2306, 2300,2294,2286,2278,2272,2262,2258,2250,2238,2234,2228,2220, 2208,2202,2192,2186,2178,2170,2164,2156,2150,2142,2134,2126, 2116,2110,2104,2096,2088,2078,2070,2062,2054,2046,2040,2034, 2026,2018,2010,2002,1994,1986,1978,1970,1962,1954,1946,1936, 1930,1922,1914,1908,1902,1894,1886,1876,1868,1860,1852,1846, 1838,1830,1822,1814,1804,1796,1790,1784,1776,1768,1760,1754, 1746,1738,1728,1720,1714,1708,1698,1692,1684,1674,1668,1656, 1656,1644,1640,1628,1624,1612,1610,1598,1596,1584,1580,1570, 1564,1554,1546,1540,1532,1526,1520,1512,1504,1496,1490,1482, 1474,1468,1462,1454,1446,1438,1432,1424,1420,1410,1404,1398, 1392,1384,1376,1370,1364,1356,1348,1342,1336,1328,1322,1316, 1308,1300,1294,1286,1280,1276,1270,1262,1254,1248,1242,1236, 1230,1222,1216,1210,1206,1198,1192,1188,1178,1172,1168,1162, 1154,1148,1144,1138,1132,1126,1120,1114,1108,1102,1096,1090, 1084,1080,1074,1068,1062,1058,1052,1048},ArrayA_IMX[256]={0}; unsigned int jm1,i; unsigned int ip,l; void main(void) { WDT_A->CTL = WDT_A_CTL_PW |WDT_A_CTL_HOLD; VCORE(); CLK(); P1DIR |= BIT5; //CLK--AD7352 OUTPUT DIRECTION P1DIR |= BIT7; //CHIP SELECT--AD7352 OUTPUT DIRECTION P5DIR &= ~BIT0; //SDATAA--AD7352 INPUT DIRECTION P5.0 P5DIR &= ~BIT2; //SDATAB--AD7352 INPUT DIRECTION P5.2 while(1) { bit_reversal(ArrayA); fft(ArrayA,ArrayA_IMX); } } void bit_reversal(long double REX[]) { int i,i2,n,m; int tx,k,j; n = 1; m=8; for (i=0;i<m;i++) { n *= 2; } i2 = n >> 1; j = 0; for (i=0;i<n-1;i++) { if (i < j) { tx = REX[i]; //ty = IMX[i]; REX[i] = REX[j]; //IMX[i] = IMX[j]; REX[j] = tx; //IMX[j] = ty; } k = i2; while (k <= j) { j -= k; k >>= 1; } j += k; } } void fft(long double REX[],long double IMX[]) { N = 256; nm1 = N - 1; nd2 = N / 2; m = log10l(N) / log10l(2); j = nd2; for (l = 1; l <= m; l++) { le = powl(2, l); le2 = le / 2; ur = 1; ui = 0; // Calculate sine and cosine values sr = cosl(M_PI/le2); si = -sinl(M_PI/le2); // Loop for each sub DFT for (j = 1; j <= le2; j++) { jm1 = j - 1; // Loop for each butterfly for (i = jm1; i <= nm1; i += le) { ip = i + le2; tr = REX[ip]*ur - IMX[ip]*ui; ti = REX[ip]*ui + IMX[ip]*ur; REX[ip] = REX[i] - tr; IMX[ip] = IMX[i] - ti; REX[i] = REX[i] + tr; IMX[i] = IMX[i] + ti; } tr = ur; ur = tr*sr - ui*si; ui = tr*si + ui*sr; } } }
How to get the hash key?
When I solve this question(149. Max Points on a Line) on leetcode, it have a bug when met this case: Input [[0,0],[94911151,94911150],[94911152,94911151]] Output 3 Expected 2 This is my code: /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution { public: int maxPoints(vector<Point>& points) { int size = points.size(); int ans = 0; if (size == 0) return 0; unordered_map<double, int> mp; double k; for (int i = 0; i < size; ++i) { int num = 0; for (int j = i + 1; j < size; ++j) { if (points[i].x == points[j].x && points[i].y == points[j].y) { num++; continue; } // my question in below code. // how can I get the hash key according to slope if (points[j].x - points[i].x != 0) k = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x); // calculate the slope. else k = INT_MAX; mp[k]++; } if (mp[k] == 0) mp[k] = 1, num--; for (auto it = mp.begin(); it != mp.end(); ++it) { if (it->second > ans) { ans = it->second; ans += num; } } mp.clear(); } return ans+1; } }; In above test case, when it calculate the slope with [0,0] and [94911151,94911150] it comeback k = 1. So I want to know how to get the right hash key to solve this problem?
imregionalmax matlab function's equivalent in opencv
I have an image of connected components(circles filled).If i want to segment them i can use watershed algorithm.I prefer writing my own function for watershed instead of using the inbuilt function in OPENCV.I have successfu How do i find the regionalmax of objects using opencv?
I wrote a function myself. My results were quite similar to MATLAB, although not exact. This function is implemented for CV_32F but it can easily be modified for other types. I mark all the points that are not part of a minimum region by checking all the neighbors. The remaining regions are either minima, maxima or areas of inflection. I use connected components to label each region. I check each region for any point belonging to a maxima, if yes then I push that label into a vector. Finally I sort the bad labels, erase all duplicates and then mark all the points in the output as not minima. All that remains are the regions of minima. Here is the code: // output is a binary image // 1: not a min region // 0: part of a min region // 2: not sure if min or not // 3: uninitialized void imregionalmin(cv::Mat& img, cv::Mat& out_img) { // pad the border of img with 1 and copy to img_pad cv::Mat img_pad; cv::copyMakeBorder(img, img_pad, 1, 1, 1, 1, IPL_BORDER_CONSTANT, 1); // initialize binary output to 2, unknown if min out_img = cv::Mat::ones(img.rows, img.cols, CV_8U)+2; // initialize pointers to matrices float* in = (float *)(img_pad.data); uchar* out = (uchar *)(out_img.data); // size of matrix int in_size = img_pad.cols*img_pad.rows; int out_size = img.cols*img.rows; int x, y; for (int i = 0; i < out_size; i++) { // find x, y indexes y = i % img.cols; x = i / img.cols; neighborCheck(in, out, i, x, y, img_pad.cols); // all regions are either min or max } cv::Mat label; cv::connectedComponents(out_img, label); int* lab = (int *)(label.data); in = (float *)(img.data); in_size = img.cols*img.rows; std::vector<int> bad_labels; for (int i = 0; i < out_size; i++) { // find x, y indexes y = i % img.cols; x = i / img.cols; if (lab[i] != 0) { if (neighborCleanup(in, out, i, x, y, img.rows, img.cols) == 1) { bad_labels.push_back(lab[i]); } } } std::sort(bad_labels.begin(), bad_labels.end()); bad_labels.erase(std::unique(bad_labels.begin(), bad_labels.end()), bad_labels.end()); for (int i = 0; i < out_size; ++i) { if (lab[i] != 0) { if (std::find(bad_labels.begin(), bad_labels.end(), lab[i]) != bad_labels.end()) { out[i] = 0; } } } } int inline neighborCleanup(float* in, uchar* out, int i, int x, int y, int x_lim, int y_lim) { int index; for (int xx = x - 1; xx < x + 2; ++xx) { for (int yy = y - 1; yy < y + 2; ++yy) { if (((xx == x) && (yy==y)) || xx < 0 || yy < 0 || xx >= x_lim || yy >= y_lim) continue; index = xx*y_lim + yy; if ((in[i] == in[index]) && (out[index] == 0)) return 1; } } return 0; } void inline neighborCheck(float* in, uchar* out, int i, int x, int y, int x_lim) { int indexes[8], cur_index; indexes[0] = x*x_lim + y; indexes[1] = x*x_lim + y+1; indexes[2] = x*x_lim + y+2; indexes[3] = (x+1)*x_lim + y+2; indexes[4] = (x + 2)*x_lim + y+2; indexes[5] = (x + 2)*x_lim + y + 1; indexes[6] = (x + 2)*x_lim + y; indexes[7] = (x + 1)*x_lim + y; cur_index = (x + 1)*x_lim + y+1; for (int t = 0; t < 8; t++) { if (in[indexes[t]] < in[cur_index]) { out[i] = 0; break; } } if (out[i] == 3) out[i] = 1; }
The following listing is a function similar to Matlab's "imregionalmax". It looks for at most nLocMax local maxima above threshold, where the found local maxima are at least minDistBtwLocMax pixels apart. It returns the actual number of local maxima found. Notice that it uses OpenCV's minMaxLoc to find global maxima. It is "opencv-self-contained" except for the (easy to implement) function vdist, which computes the (euclidian) distance between points (r,c) and (row,col). input is one-channel CV_32F matrix, and locations is nLocMax (rows) by 2 (columns) CV_32S matrix. int imregionalmax(Mat input, int nLocMax, float threshold, float minDistBtwLocMax, Mat locations) { Mat scratch = input.clone(); int nFoundLocMax = 0; for (int i = 0; i < nLocMax; i++) { Point location; double maxVal; minMaxLoc(scratch, NULL, &maxVal, NULL, &location); if (maxVal > threshold) { nFoundLocMax += 1; int row = location.y; int col = location.x; locations.at<int>(i,0) = row; locations.at<int>(i,1) = col; int r0 = (row-minDistBtwLocMax > -1 ? row-minDistBtwLocMax : 0); int r1 = (row+minDistBtwLocMax < scratch.rows ? row+minDistBtwLocMax : scratch.rows-1); int c0 = (col-minDistBtwLocMax > -1 ? col-minDistBtwLocMax : 0); int c1 = (col+minDistBtwLocMax < scratch.cols ? col+minDistBtwLocMax : scratch.cols-1); for (int r = r0; r <= r1; r++) { for (int c = c0; c <= c1; c++) { if (vdist(Point2DMake(r, c),Point2DMake(row, col)) <= minDistBtwLocMax) { scratch.at<float>(r,c) = 0.0; } } } } else { break; } } return nFoundLocMax; }
I do not know if it is what you want, but in my answer to this post, I gave some code to find local maxima (peaks) in a grayscale image (resulting from distance transform). The approach relies on subtracting the original image from the dilated image and finding the zero pixels). I hope it helps, Good luck
I had the same problem some time ago, and the solution was to reimplement the imregionalmax algorithm in OpenCV/Cpp. It is not that complicated, because you can find the C++ source code of the function in the Matlab distribution. (somewhere in toolbox). All you have to do is to read carefully and understand the algorithm described there. Then rewrite it or remove the matlab-specific checks and you'll have it.
Looking for SLAB6 implementation
I'm looking to implement SLAB6 into my raycaster, especially the kv6 support for voxelmodels. However the SLAB6 source by Ken Silverman is totally unreadably (mostly ASM) so I was hoping someone could point me to a proper C / Java source to load kv6 models or maybe to explain me the workings in some pseudocode preferably (since I want to know how to support the kv6, I know how it works). Thanks, Kaj EDIT: the implementation would be in Java.
I found some code in an application called VoxelGL (author not mentioned in sourcecode): void CVoxelWorld::generateSlabFromData(unsigned char *data, VoxelData *vdata, Slab *slab) { int currentpattern = 1; int i = 0; int n, totalcount, v, count; n = 0; v = 0; while (1) { while (data[i] == currentpattern) { if (currentpattern == 1) v++; i++; if (i == 256) break; } n++; if (i == 256) { if (currentpattern == 0) n--; break; } currentpattern ^= 1; } slab->nentries = n; if (slab->description != 0)delete [] slab->description; if (slab->data != 0)delete [] slab->data; slab->description = new int[n]; slab->data = new VoxelData[v]; totalcount = 0; v = 0; currentpattern = 1; for (i = 0; i < n; i++) { count = 0; while (data[totalcount] == currentpattern) { count++; totalcount++; if (totalcount == 256) break; } slab->description[i] = count-1; if (i % 2 == 0) { memcpy(slab->data + v, vdata + totalcount - count, 3 * count); v += count; } currentpattern ^= 1; } } And: #define clustersize 8 Slab *CVoxelWorld::getSlab(int x, int z) { int xgrid = x / clustersize; int ygrid = z / clustersize; int clusteroffset = xgrid * 1024 * clustersize + ygrid * clustersize * clustersize; return &m_data[clusteroffset + (x & (clustersize - 1)) + (z & (clustersize - 1)) * clustersize]; } And: int CVoxelWorld::isSolid(int x, int y, int z) { Slab *slab; if (y < 0 || y > 256) return 0; slab = getSlab(x, z); int counter = 0; for (int i = 0; i < slab->nentries; i++) { int height = slab->description[i] + 1; if (i % 2 == 0) { if (y >= counter && y < counter + height) return 1; } counter += height; } return 0; }
Teaching a Neural Net: Bipolar XOR
I'm trying to to teach a neural net of 2 inputs, 4 hidden nodes (all in same layer) and 1 output node. The binary representation works fine, but I have problems with the Bipolar. I can't figure out why, but the total error will sometimes converge to the same number around 2.xx. My sigmoid is 2/(1+ exp(-x)) - 1. Perhaps I'm sigmoiding in the wrong place. For example to calculate the output error should I be comparing the sigmoided output with the expected value or with the sigmoided expected value? I was following this website here: http://galaxy.agh.edu.pl/~vlsi/AI/backp_t_en/backprop.html , but they use different functions then I was instructed to use. Even when I did try to implement their functions I still ran into the same problem. Either way I get stuck about half the time at the same number (a different number for different implementations). Please tell me if I have made a mistake in my code somewhere or if this is normal (I don't see how it could be). Momentum is set to 0. Is this a common 0 momentum problem? The error functions we are supposed to be using are: if ui is an output unit Error(i) = (Ci - ui ) * f'(Si ) if ui is a hidden unit Error(i) = Error(Output) * weight(i to output) * f'(Si) public double sigmoid( double x ) { double fBipolar, fBinary, temp; temp = (1 + Math.exp(-x)); fBipolar = (2 / temp) - 1; fBinary = 1 / temp; if(bipolar){ return fBipolar; }else{ return fBinary; } } // Initialize the weights to random values. private void initializeWeights(double neg, double pos) { for(int i = 0; i < numInputs + 1; i++){ for(int j = 0; j < numHiddenNeurons; j++){ inputWeights[i][j] = Math.random() - pos; if(inputWeights[i][j] < neg || inputWeights[i][j] > pos){ print("ERROR "); print(inputWeights[i][j]); } } } for(int i = 0; i < numHiddenNeurons + 1; i++){ hiddenWeights[i] = Math.random() - pos; if(hiddenWeights[i] < neg || hiddenWeights[i] > pos){ print("ERROR "); print(hiddenWeights[i]); } } } // Computes output of the NN without training. I.e. a forward pass public double outputFor ( double[] argInputVector ) { for(int i = 0; i < numInputs; i++){ inputs[i] = argInputVector[i]; } double weightedSum = 0; for(int i = 0; i < numHiddenNeurons; i++){ weightedSum = 0; for(int j = 0; j < numInputs + 1; j++){ weightedSum += inputWeights[j][i] * inputs[j]; } hiddenActivation[i] = sigmoid(weightedSum); } weightedSum = 0; for(int j = 0; j < numHiddenNeurons + 1; j++){ weightedSum += (hiddenActivation[j] * hiddenWeights[j]); } return sigmoid(weightedSum); } //Computes the derivative of f public static double fPrime(double u){ double fBipolar, fBinary; fBipolar = 0.5 * (1 - Math.pow(u,2)); fBinary = u * (1 - u); if(bipolar){ return fBipolar; }else{ return fBinary; } } // This method is used to update the weights of the neural net. public double train ( double [] argInputVector, double argTargetOutput ){ double output = outputFor(argInputVector); double lastDelta; double outputError = (argTargetOutput - output) * fPrime(output); if(outputError != 0){ for(int i = 0; i < numHiddenNeurons + 1; i++){ hiddenError[i] = hiddenWeights[i] * outputError * fPrime(hiddenActivation[i]); deltaHiddenWeights[i] = learningRate * outputError * hiddenActivation[i] + (momentum * lastDelta); hiddenWeights[i] += deltaHiddenWeights[i]; } for(int in = 0; in < numInputs + 1; in++){ for(int hid = 0; hid < numHiddenNeurons; hid++){ lastDelta = deltaInputWeights[in][hid]; deltaInputWeights[in][hid] = learningRate * hiddenError[hid] * inputs[in] + (momentum * lastDelta); inputWeights[in][hid] += deltaInputWeights[in][hid]; } } } return 0.5 * (argTargetOutput - output) * (argTargetOutput - output); }
General coding comments: initializeWeights(-1.0, 1.0); may not actually get the initial values you were expecting. initializeWeights should probably have: inputWeights[i][j] = Math.random() * (pos - neg) + neg; // ... hiddenWeights[i] = (Math.random() * (pos - neg)) + neg; instead of: Math.random() - pos; so that this works: initializeWeights(0.0, 1.0); and gives you initial values between 0.0 and 1.0 rather than between -1.0 and 0.0. lastDelta is used before it is declared: deltaHiddenWeights[i] = learningRate * outputError * hiddenActivation[i] + (momentum * lastDelta); I'm not sure if the + 1 on numInputs + 1 and numHiddenNeurons + 1 are necessary. Remember to watch out for rounding of ints: 5/2 = 2, not 2.5! Use 5.0/2.0 instead. In general, add the .0 in your code when the output should be a double. Most importantly, have you trained the NeuralNet long enough? Try running it with numInputs = 2, numHiddenNeurons = 4, learningRate = 0.9, and train for 1,000 or 10,000 times. Using numHiddenNeurons = 2 it sometimes get "stuck" when trying to solve the XOR problem. See also XOR problem - simulation