numerical

How do I save a floating-point number in 2 bytes?

社会主义新天地 提交于 2019-11-30 05:25:11
问题 Yes I'm aware of the IEEE-754 half-precision standard, and yes I'm aware of the work done in the field. Put very simply, I'm trying to save a simple floating point number (like 52.1 , or 1.25 ) in just 2 bytes. I've tried some implementations in Java and in C# but they ruin the input value by decoding a different number. You feed in 32.1 and after encode-decode you get 32.0985 . Is there ANY way I can store floating point numbers in just 16-bits without ruining the input value? Thanks very

Derivatives in C/C++?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 03:09:18
I have some expressions such as x^2+y^2 that I'd like to use for some math calculations. One of the things I'd like to do is to take partial derivatives of the expressions. So if f(x,y) = x^2 + y^2 then the partial of f with respect to x would be 2x , the partial with respect to y would be 2y . I wrote a dinky function using a finite differences method but I'm running into lots of problems with floating point precision. For example, I end up with 1.99234 instead of 2 . Are there any libraries that support symbolic differentiation? Any other suggestions? I've implemented such libraries in

How do I convert certain columns of a data frame to become factors? [duplicate]

China☆狼群 提交于 2019-11-29 20:53:49
Possible Duplicate: identifying or coding unique factors using R I'm having some trouble with R. I have a data set similar to the following, but much longer. A B Pulse 1 2 23 2 2 24 2 2 12 2 3 25 1 1 65 1 3 45 Basically, the first 2 columns are coded. A has 1, 2 which represent 2 different weights. B has 1, 2, 3 which represent 3 different times. As they are coded numerical values, R will treat them as numerical variables. I need to use the factor function to convert these variables into factors. Help? Here's an example: #Create a data frame > d<- data.frame(a=1:3, b=2:4) > d a b 1 1 2 2 2 3 3

Open source alternative to MATLAB's fmincon function?

末鹿安然 提交于 2019-11-29 19:45:00
Is there an open-source alternative to MATLAB's fmincon function for constrained linear optimization? I'm rewriting a MATLAB program to use Python / NumPy / SciPy and this is the only function I haven't found an equivalent to. A NumPy-based solution would be ideal, but any language will do. Is your problem convex? Linear? Non-linear? I agree that SciPy.optimize will probably do the job, but fmincon is a sort of bazooka for solving optimization problems, and you'll be better off if you can confine it to one of the categories below (in increasing level of difficulty to solve efficiently) Linear

How to write fast (low level) code? [closed]

狂风中的少年 提交于 2019-11-29 18:49:42
I would like to learn more about low level code optimization, and how to take advantage of the underlying machine architecture. I am looking for good pointers on where to read about this topic. More details: I am interested in optimization in the context of scientific computing (which is a lot of number crunching but not only ) in low level languages such as C/C++. I am in particular interested in optimization methods that are not obvious unless one has a good understanding of how the machine works (which I don't---yet). For example, it's clear that a better algorithm is faster, without

Numeric vector operator overload+ rvalue reference parameter

落爺英雄遲暮 提交于 2019-11-29 18:29:50
问题 I have the numeric vector template class below (vector for numerical calculations). I am trying make it possible to write D=A+B+C where all variables are Vector objects. A , B and C should not be modified. My idea is to use Vector operator+(Vector&& B) so that after (hopefully) an Rvalue Vector has been returned from B+C , all subsequent additions are stored in that object i.e. steal the storage of the Rvalue for all subsequent additions. This is in order to eliminate creation of new objects

UMFPACK and BOOST's uBLAS Sparse Matrix

£可爱£侵袭症+ 提交于 2019-11-29 15:25:37
问题 I am using Boost's uBLAS in a numerical code and have a 'heavy' solver in place: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?LU_Matrix_Inversion The code works excellently, however, it is painfully slow. After some research, I found UMFPACK, which is a sparse matrix solver (among other things). My code generates large sparse matrices which I need to invert very frequently (more correctly solve, the value of the inverse matrix is irrelevant), so UMFPACk and BOOST's Sparse

Jacobi iteration doesn't end

早过忘川 提交于 2019-11-29 08:21:59
I'm trying to implement the Jacobi iteration in MATLAB but am unable to get it to converge. I have looked online and elsewhere for working code for comparison but am unable to find any that is something similar to my code and still works. Here is what I have: function x = Jacobi(A,b,tol,maxiter) n = size(A,1); xp = zeros(n,1); x = zeros(n,1); k=0; % number of steps while(k<=maxiter) k=k+1; for i=1:n xp(i) = 1/A(i,i)*(b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x(i+1:n)); end err = norm(A*xp-b); if(err<tol) x=xp; break; end x=xp; end This just blows up no matter what A and b I use. It's probably a

Dealing with floating point errors in .NET

纵然是瞬间 提交于 2019-11-29 06:50:44
I'm working on a scientific computation & visualization project in C#/.NET, and we use double s to represent all the physical quantities. Since floating-point numbers always include a bit of rounding, we have simple methods to do equality comparisons, such as: static double EPSILON = 1e-6; bool ApproxEquals(double d1, double d2) { return Math.Abs(d1 - d2) < EPSILON; } Pretty standard. However, we constantly find ourselves having to adjust the magnitude of EPSILON as we encounter situations in which the error of "equal" quantities is greater than we had anticipated. For example, if you multiply

Any way faster than pow() to compute an integer power of 10 in C++?

谁都会走 提交于 2019-11-29 01:40:16
问题 I know power of 2 can be implemented using << operator. What about power of 10? Like 10^5? Is there any way faster than pow(10,5) in C++? It is a pretty straight-forward computation by hand. But seems not easy for computers due to binary representation of the numbers... Let us assume I am only interested in integer powers, 10^n, where n is an integer. 回答1: Something like this: int quick_pow10(int n) { static int pow10[10] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,