I am trying to sole these two problems and any input would be appreciated.
My solutions to this one works but the output object keep repeating the "Sample" before the object on an array.
(1) Latin Hypercube Sampling
Write a function that produces N pseudo-random samples from a real space D. The function should take in as arguments:
- the number of samples to produce (N)
- the bounds of each dimension (dmin, dmax)
The function should return an array of objects representing the whole set of random number tuples. For example:
result = [ { "d1":1, "d2":3 }, { "d1":2, "d2":1 }, { "d1":3, "d2":2 } ];
// Create the function.
const hypercube = (N, dmin, dmax) => {
const samp = [];
for(let i = 0; i <= N; i ++){
const min = Math.ceil(dmin),
max = Math.floor(dmax);
// let build = new latin(min, max);
// samp.push(build);
samp.push(new Sample(min, max));
}
console.log(samp);
};
// Run the function.
hypercube(2, 1, 6);
// This is the constructor function.
function Sample (min, max) {
this.d1 = Math.floor(Math.random() * (max - min + 1) + min);
this.d2 = Math.floor(Math.random() * (max - min + 1) + min);
}
This is the second part, my solution is similar to the first one. I created an array of letters and selected one randomly from that array.
(2) Combinatorics
Extend the function to allow for combinatorial data types, i.e., values from a fixed, unordered set. The combinatorial data types should be represented a string. The function should take in as arguments:
- the number of samples to produce (N)
- the configuration of each dimension:
- - for a real number this should be bounds (dmin, dmax)
- - for a combinatorial value this should be an array of possible values ( [ ... ] )
For example, the function might produce results for three dimensions: the first two being real numbers in the range [1 - 3] and the third being a combinatorial set ["A", "B", "C"].
As before: the function should return an array of objects. For example:
// Create the function.
const hypercube = (N, dmin, dmax) => {
const samp = [];
for(let i = 0; i <= N; i ++){
const min = Math.ceil(dmin),
max = Math.floor(dmax);
// let build = new latin(min, max);
// samp.push(build);
samp.push(new Sample(min, max));
}
console.log(samp);
};
// Run the function.
hypercube(2, 1, 6);
// This is the constructor function.
function Sample (min, max) {
this.d1 = Math.floor(Math.random() * (max - min + 1) + min);
this.d2 = Math.floor(Math.random() * (max - min + 1) + min);
}
result = [ { "d1":1, "d2":3, "d3":"B" }, { "d1":2, "d2":1, "d3":"A" }, { "d1":3, "d2":2, "d3":"C" } ];
I would appreciate any input.
Thank you.
Related
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 8 years ago.
I have a large amount of numeric values y in javascript. I want to group them by rounding them down to the nearest multiple of x and convert the result to a string.
How do I get around the annoying floating point precision?
For example:
0.2 + 0.4 = 0.6000000000000001
Two things I have tried:
>>> y = 1.23456789
>>> x = 0.2
>>> parseInt(Math.round(Math.floor(y/x))) * x;
1.2000000000000002
and:
>>> y = 1.23456789
>>> x = 0.2
>>> y - (y % x)
1.2000000000000002
From this post: How to deal with floating point number precision in JavaScript?
You have a few options:
Use a special datatype for decimals, like decimal.js
Format your result to some fixed number of significant digits, like this:
(Math.floor(y/x) * x).toFixed(2)
Convert all your numbers to integers
You could do something like this:
> +(Math.floor(y/x)*x).toFixed(15);
1.2
Edit: It would be better to use big.js.
big.js
A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
>> bigX = new Big(x)
>> bigY = new Big(y)
>> bigY.div(bigX).round().times(bigX).toNumber() // => 1.2
> var x = 0.1
> var y = 0.2
> var cf = 10
> x * y
0.020000000000000004
> (x * cf) * (y * cf) / (cf * cf)
0.02
Quick solution:
var _cf = (function() {
function _shift(x) {
var parts = x.toString().split('.');
return (parts.length < 2) ? 1 : Math.pow(10, parts[1].length);
}
return function() {
return Array.prototype.reduce.call(arguments, function (prev, next) { return prev === undefined || next === undefined ? undefined : Math.max(prev, _shift (next)); }, -Infinity);
};
})();
Math.a = function () {
var f = _cf.apply(null, arguments); if(f === undefined) return undefined;
function cb(x, y, i, o) { return x + f * y; }
return Array.prototype.reduce.call(arguments, cb, 0) / f;
};
Math.s = function (l,r) { var f = _cf(l,r); return (l * f - r * f) / f; };
Math.m = function () {
var f = _cf.apply(null, arguments);
function cb(x, y, i, o) { return (x*f) * (y*f) / (f * f); }
return Array.prototype.reduce.call(arguments, cb, 1);
};
Math.d = function (l,r) { var f = _cf(l,r); return (l * f) / (r * f); };
> Math.m(0.1, 0.2)
0.02
You can check the full explanation here.
Check out this link.. It helped me a lot.
http://www.w3schools.com/jsref/jsref_toprecision.asp
The toPrecision(no_of_digits_required) function returns a string so don't forget to use the parseFloat() function to convert to decimal point of required precision.
Tackling this task, I'd first find the number of decimal places in x, then round y accordingly. I'd use:
y.toFixed(x.toString().split(".")[1].length);
It should convert x to a string, split it over the decimal point, find the length of the right part, and then y.toFixed(length) should round y based on that length.
This is more or less my first attempt at writing a Javascript function and I want to convert an array of column numbers to an array of column letters
If I run testFunction I get undefined
function testFunction() {
var ui = SpreadsheetApp.getUi();
aryCLTCN(["A","C","D"])
ui.alert(aryCLTCN[3]);
}
function aryCLTCN(array) {
var columnLet = array
var output = [];
for (var i = 0, length = columnLet.length; i < length; i++) {
output[i] = [];
output[i] = CLTCN(columnLet[(i)]);
}
}
function CLTCN(letter)
{
var column = 0, length = letter.length;
for (var i = 0; i < length; i++)
{
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
There are several problems with your code.
Within function testFunction() you call aryCLTCN(["A","C","D"]) but don't assign the result to a variable, then with aryCLTCN[3] you are trying to access a property "3" of the function itself. Which isn't a syntax error because functions can have properties, but the function has no such property so you get undefined. You need something like this:
var result = aryCLTCN(["A","C","D"]);
ui.alert(result[3]);
Except note that JavaScript arrays are zero-based, which means that [3] tries to access the fourth element, but your array only has three elements.
Within function aryCLTCN(array) you create an output array but don't return it. You need to add return output;.
Also with these two lines:
output[i] = [];
output[i] = CLTCN(columnLet[(i)]);
...the first line assigns output[i] to a new empty array, but the second line overwrites that with the return value from CLTCN(columnLet[(i)]);. You can remove output[i] = [];.
Putting all that together:
function testFunction() {
// var ui = SpreadsheetApp.getUi(); // commented out for demo in browser
var result = aryCLTCN(["A","C","D"])
// using alert() instead of ui.alert() for demo here in browser
alert(result[3]); // undefined because there's no 4th element
alert(result[2]); // shows third element
}
function aryCLTCN(array) {
var columnLet = array
var output = [];
for (var i = 0, length = columnLet.length; i < length; i++) {
output[i] = CLTCN(columnLet[(i)]);
}
return output;
}
function CLTCN(letter)
{
var column = 0, length = letter.length;
for (var i = 0; i < length; i++)
{
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
testFunction();
(Note that for the purposes of having a runnable code snippet in my answer I'm using alert() instead of ui.alert(), but in your real code you would stick with ui.alert().)
You get an undefined error because you are calling the trying to access an index on a function. aryCLTCN function needs to have a return the output array and you need to assign it to a variable in your testFunction to be able to access its elements.
Although there was nothing logically or effectively wrong with your functions, I have provided another working solution below.
function testFunction() {
var ui = SpreadsheetApp.getUi();
var colArr = ["A", "B", "Z", "AA", "AZ", "ZA", "AAA"];
var nColArr = colArr.map(function(col) {
var colNum = 0;
col.split('').forEach(function(l, i) { colNum += (l.charCodeAt() - 64) * Math.pow(26, col.length - 1 - i) });
return colNum;
});
ui.alert(nColArr); //Shows all elements inside the nColArr array.
ui.alert(nColArr[3]); //Shows the 4th element inside the nColArr array.
}
Try it out:
var colArr = ["A", "B", "Z", "AA", "AZ", "ZA", "AAA"];
var nColArr = colArr.map(function(col) {
var colNum = 0;
col.split('').forEach(function(l, i) {
colNum += (l.charCodeAt() - 64) * Math.pow(26, col.length - 1 - i)
});
return colNum;
});
console.log(nColArr);
I would like to create an array of numbers and then randomize those numbers.
Here is what I've come up with. It does two things.
It gets an array from 0 to the length specified. What the main question was about.
It then randomizes the items in that array
// returns [1,2,3,4,5,6,7,8,9,10] except shuffled
var array:Array = getSequenceArray(10);
/**
* Get an array of the range of numbers from 1 to the number specified and randomize them.
* */
public function getRandomArray(count:int):Array {
// this part is not looped. yay!
var array:Array = new Array(count).map(function (item, index) { return index + 1; });
return randomizeArray(array);;
}
/**
* Randomize items in an array
* */
public function randomizeArray(original:Array, cloneArray:Boolean = true):Array {
var length:int = original.length;
var shuffledArray:Array = [];
var newArray:Array = original.slice();
var randomNumber:Number;
// this is still using a loop. this loop is less of an issue than the first
while (length) {
randomNumber = Math.floor(Math.random() * length);
shuffledArray.push(newArray.splice(randomNumber, 1)[0]);
length--;
}
if (!cloneArray) {
original.splice(0, shuffledArray.length);
original.push.apply(this, shuffledArray);
return original;
}
return shuffledArray;
}
I know it was asked a thousand times before, but I still can't find a solution.
Searching SO, I indeed found the algorithm for it, but lacking the mathematical knowledge required to truly understand it, I am helplessly lost!
To start with the beginning, my goal is to compute an entire spectrogram and save it to an image in order to use it for a visualizer.
I tried using Sound.computeSpectrum, but this requires to play the sound and wait for it to end, I want to compute the spectrogram in a way shorter time than that will require to listen all the song. And I have 2 hours long mp3s.
What I am doing now is to read the bytes from a Sound object, the separate into two Vectors(.); Then using a timer, at each 100 ms I call a function (step1) where I have the implementation of the algorithm, as follows:
for each vector (each for a channel) I apply the hann function to the elements;
for each vector I nullify the imaginary part (I have a secondary vector for that)
for each vector I apply FFT
for each vector I find the magnitude for the first N / 2 elements
for each vector I convert squared magnitude to dB scale
end.
But I get only negative values, and only 30 percent of the results might be useful (in the way that the rest are identical)
I will post the code for only one channel to get rid off the "for each vector" part.
private var N:Number = 512;
private function step1() : void
{
var xReLeft:Vector.<Number> = new Vector.<Number>(N);
var xImLeft:Vector.<Number> = new Vector.<Number>(N);
var leftA:Vector.<Number> = new Vector.<Number>(N);
// getting sample range
leftA = this.channels.left.slice(step * N, step * (N) + (N));
if (leftA.length < N)
{
stepper.removeEventListener(TimerEvent.TIMER, getFreq100ms);
return;
}
else if (leftA.length == 0)
{
stepper.removeEventListener(TimerEvent.TIMER, getFreq100ms);
return;
}
var i:int;
// hann window function init
m_win = new Vector.<Number>(N);
for ( var i:int = 0; i < N; i++ )
m_win[i] = (4.0 / N) * 0.5 * (1 - Math.cos(2 * Math.PI * i / N));
// applying hann window function
for ( i = 0; i < N; i++ )
{
xReLeft[i] = m_win[i]*leftA[i];
//xReRight[i] = m_win[i]*rightA[i];
}
// nullify the imaginary part
for ( i = 0; i < N; i++ )
{
xImLeft[i] = 0.0;
//xImRight[i] = 0.0;
}
var magnitutel:Vector.<Number> = new Vector.<Number>(N);
fftl.run( xReLeft, xImLeft );
current = xReLeft;
currf = xImLeft;
for ( i = 0; i < N / 2; i++ )
{
var re:Number = xReLeft[i];
var im:Number = xImLeft[i];
magnitutel[i] = Math.sqrt(re * re + im * im);
}
const SCALE:Number = 20 / Math.LN10;
var l:uint = this.total.length;
for ( i = 0; i < N / 2; i++ )
{
magnitutel[i] = SCALE * Math.log( magnitutel[i] + Number.MIN_VALUE );
}
var bufferl:Vector.<Number> = new Vector.<Number>();
for (i = 0; i < N / 2 ; i++)
{
bufferl[i] = magnitutel[i];
}
var complete:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
complete[0] = bufferl;
this.total[step] = complete;
this.step++;
}
This function is executed in the event dispatched by the timer (stepper).
Obviously I do something wrong, as I said I have only negative values and further more values range between 1 and 7000 (at least).
I want to thank you in advance for any help.
With respect,
Paul
Negative dB values are OK. Just add a constant (representing your volume control) until the number of points you want to color become positive. The remaining values that stay negative are usually just displayed or colored as black in a spectrogram. No matter how negative (as they might just be the FFT's numerical noise, which can be a huge negative dB number or even NaN or -Inf for log(0)).
I have a number array and I'd like to calculate the median.
When the array is odd, the calculation is OK, when it's even strange number comes up.
private var numbers:String = "2,5,3,4,6,1";
private var array:Array = numbers.split(",");
private function getMedian(array:Array):Number {
var sortnums:Array = array.sort(Array.NUMERIC);
var length:Number = sortnums.length;
var mid1:Number; var mid2:Number; var median:Number;
if(length % 2 == 0){
mid1 = length / 2; trace("mid1: "+mid1);
mid2= ((length - 1) / 2)-0.5; trace("mid2: "+mid2);
trace ("mid1: "+sortnums[mid1]+", mid2: "+sortnums[mid2]);
median = (sortnums[mid1] + sortnums[mid2]) / 2;
}else{
mid1 = (length / 2)-0.5
median = sortnums[mid1]
}
trace (median);
return median;
}
The result is 21.5, but should be 3.5
mid1 and mid2 are a position in the array.
Could somebody help?
Try this (for tidyness):
function getMedian(plug:Array):Number
{
// Even length.
if(plug.length % 2 == 0)
{
var a:Number = plug[int(plug.length / 2) - 1];
var b:Number = plug[int(plug.length / 2)];
return (a + b) / 2;
}
// Odd length.
return plug[int(plug.length / 2)];
}
// Tests.
trace(getMedian([2,5,3,4,6,1])); // 3.5
trace(getMedian([2,5,3,4,6])); // 3
By the way, I'm fairly certain your code is working fine, the problem is that you're doing this as a string concatenation rather than a number sum here:
median = (sortnums[mid1] + sortnums[mid2]) / 2;
Meaning you're literally adding the string 1.5 onto 2 and getting 21.5 rather than 3.5.
Try amending your code so it reads like so:
median = (Number(sortnums[mid1]) + Number(sortnums[mid2])) / 2;