numerical

Round number to specified number of digits

时光毁灭记忆、已成空白 提交于 2019-12-03 04:55:30
Is there a simple function to round a Double or Float to a specified number of digits? I've searched here and on Hoogle (for (Fractional a) => Int -> a -> a ), but haven't found anything. Not sure whether any standard function exists, but you can do it this way: (fromInteger $ round $ f * (10^n)) / (10.0^^n) It depends on what you are going to do with the rounded number. If you want to use it in calculations, you should use Data.Decimal from Decimal library. If you want just to format the number nicely, you should use Text.Printf from the standard library ( base package). λ: ((/100) $

C/C++ for Python programmer [closed]

▼魔方 西西 提交于 2019-12-03 04:50:14
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 months ago . I have to switch from Python to C/C++. Do you know a quick "reference tutorial" or something like that to have a reference to how to start? For example something like the Numpy and Scipy tutorials. I have read a lot of "documentation", for example C++ for dummies the K&R C Programming Language a lot of blog

What's a good way to add a large number of small floats together?

让人想犯罪 __ 提交于 2019-12-03 02:42:08
Say you have 100000000 32-bit floating point values in an array, and each of these floats has a value between 0.0 and 1.0. If you tried to sum them all up like this result = 0.0; for (i = 0; i < 100000000; i++) { result += array[i]; } you'd run into problems as result gets much larger than 1.0. So what are some of the ways to more accurately perform the summation? Sounds like you want to use Kahan Summation . According to Wikipedia, The Kahan summation algorithm (also known as compensated summation ) significantly reduces the numerical error in the total obtained by adding a sequence of finite

How do I approximate the Jacobian and Hessian of a function numerically?

≯℡__Kan透↙ 提交于 2019-12-02 21:05:46
I have a function in Python: def f(x): return x[0]**3 + x[1]**2 + 7 # Actually more than this. # No analytical expression It's a scalar valued function of a vector. How can I approximate the Jacobian and Hessian of this function in numpy or scipy numerically? (Updated in late 2017 because there's been a lot of updates in this space.) Your best bet is probably automatic differentiation . There are now many packages for this, because it's the standard approach in deep learning: Autograd works transparently with most numpy code. It's pure-Python, requires almost no code changes for typical

Generate letters to represent number using ruby?

和自甴很熟 提交于 2019-12-02 18:22:06
I would like to generate a sequence of letters i.e. "A", "DE" "GJE", etc. that correspond to a number. The first 26 are pretty easy so 3 returns "C", 26 returns "Z", and 27 would return "AA", 28 "AB", and so on. The thing I can't quite figure out is how to do this so it will handle any number passed in. So if I pass in 4123 I should get back some combination of 3 letters since (26 * 26 * 26) allows for up to +17,000 combinations. Any suggestions? class Numeric Alph = ("a".."z").to_a def alph s, q = "", self (q, r = (q - 1).divmod(26)); s.prepend(Alph[r]) until q.zero? s end end 3.alph # => "c"

C/C++ for Python programmer [closed]

断了今生、忘了曾经 提交于 2019-12-02 17:20:58
I have to switch from Python to C/C++. Do you know a quick "reference tutorial" or something like that to have a reference to how to start? For example something like the Numpy and Scipy tutorials. I have read a lot of "documentation", for example C++ for dummies the K&R C Programming Language a lot of blog and online documentation such as: http://eli.thegreenplace.net/2010/01/11/pointers-to-arrays-in-c/, http://newdata.box.sk/bx/c/ tons of Q&A here on StackOverflow ... but it's still not clear to me even how to do start porting to C/C++ something like: #!/usr/bin/env python import time import

sorting numerically by first row

一个人想着一个人 提交于 2019-12-02 17:01:04
问题 I have a file with almost 900 lines in excel that I've saved as a tab deliminated .txt file. I'd like to sort the text file by the numbers given in the first column (they range between 0 and 2250). The other columns are both numbers and letters of varying length eg. myfile.txt: 0251 abcd 1234,24 bcde 2240 efgh 2345,98 ikgpppm 0001 lkjsi 879,09 ikol I've tried sort -k1 -n myfile.txt > myfile_num.txt but I just get an identical file with new name. I'd like to get: myfile_num.txt 0001 lkjsi 879

sorting numerically by first row

瘦欲@ 提交于 2019-12-02 11:10:51
I have a file with almost 900 lines in excel that I've saved as a tab deliminated .txt file. I'd like to sort the text file by the numbers given in the first column (they range between 0 and 2250). The other columns are both numbers and letters of varying length eg. myfile.txt: 0251 abcd 1234,24 bcde 2240 efgh 2345,98 ikgpppm 0001 lkjsi 879,09 ikol I've tried sort -k1 -n myfile.txt > myfile_num.txt but I just get an identical file with new name. I'd like to get: myfile_num.txt 0001 lkjsi 879,09 ikol 0251 abcd 1234,24 bcde 2240 efgh 2345,98 ikgpppm What am I doing wrong? I'm guessing that it's

Perl: Numerical sort of arrays in a hash 2 (Schwarzian Transform)

五迷三道 提交于 2019-12-02 10:41:42
This is actually a follow-up of this thread: Perl: Numerical sort of arrays in a hash I couldn't edit the original question because my current code is a bit different, so I'm just asking this as another question. Okay after using the Schwarzian Transform, I have this: my @mylines =("0.899 0.92 cat", "9.999 0.001 dog", "-0.52 0.3 humpty", "13.52 0.09 bumbo", "-1.52 0.98 nanny", "3.52 0.34 lala"); my @sorted = map { join ' ', @$_ } reverse sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] } map { [ split ] } (@mylines); print "$_\n" for @sorted; I would expect the output to be sorted first by

The number of correct decimal digits in a product of doubles with a large number of terms

早过忘川 提交于 2019-12-02 08:04:47
问题 What is a tight lower-bound on the size of the set of irrational numbers, N , expressed as doubles in Matlab on a 64-bit machine, that I multiply together while having confidence in k decimal digits of the product? What precision, for example could I expect after multiplying together ~10^12 doubles encoding different random chunks of pi? 回答1: For 64 bit floating point numbers, assuming the standard IEEE 754, has 52+1 bits of mantissa. That means relative precision is between 1.0000...0 and 1