VB 2010, Complex numbers - vb.net

Hi does anybody know if is possible to utilise formulas containing complex numbers within basic programming. Part of the formula im trying to implement is i(e^2-e^2-2), e represents exponential and i the imaginary number. Any help appreciated.

Add a reference to the System.Numerics library to your project (Go to MyProject->References->Add->Assemblies->Search the list for System.Numerics). You can also import the namespace in your source by using Imports System.Numerics.
In this library you can find functions to compute complex numbers.
You define a complex number for example with the line
Dim MyComplex as New Complex(Realpart, ImaginaryPart)
To compute the exponential you would use
Dim Exped as Complex = Complex.Exp(MyComplex)
and so on. There are functions for most operations and the complex numbers support general operators like +, -, *, / as well.
For your specific problem you would just use
Dim result as New Complex(0, Math.Exp(2)-Math.Exp(2)-2)
This works because i*x is a complex number with a real part of zero and an imaginary part of x. I'm not judging how much sense your definition makes :-)

Related

What is the difference between String.Substring and Mid?

I am really confused between Mid and Substring in VB.NET. Can anyone help me understand the difference with the help of a program? It will be really appreciated.
You can read the documentation for Mid and for Substring
The biggest difference arises from Mid being a legacy adapter function intended either to help VB6 code work if pasted into a vb.net project, or to provide VB6 programmers with some familiar functionality while they switch to using the modern .NET equivalent (Substring)
As a legacy function, Mid adopts the VB6 notions of strings being one-based indexing, rather than zero based (used by nearly everything else .NET) so anything you Mid should have a start parameter that is 1 greater than anything you Substring
Mid("Hellow World",1,5) 'returns Hello
Substring("Hellow World",0,5) 'returns Hello
Substring has a corollary, Remove, which removes chars after a certain point like Left used to. Ditching Left/Mid/Right in favour of Substring/Remove makes it easier to understand what to use/what will happen if the string passed in is in a right-to-left language

Visual Basics Calculater hexadecimal and binary numbers

as an exercise for university we need to create a calculator with visual basic.
It has to be able to work with hexadecimal numbers and binary numbers.
My plan is to translate those to normal numbers, calculate and than translate them back, so i can use the simple +, -, *, / .
Unfortunately i wasn't able to find any translation between the number systems.
Is there anybody, who could send one in? It'd help me quite a lot.
Greetings Phil
The Convert class:
Dim fromHex As Long = Convert.ToInt64("beef", 16)
Dim fromBin As Long = Convert.ToInt64("1011110", 2)

GMP Library: How to check first m-bit of a big integer

I'm using gmp (big integer library)
I have a value b I need to check whether first m-bit of it, is zero.
I know if I convert b to string I can do checking, but it's not efficient.
Question 1: I need to know whether the library has a function that returns first m-bit of a big integer.
To encode a big integer c, I convert it to string and then concatenate it with m zeros, e.g. imagine c in binary is 11, then I encode as: 1100000000
Question 2: I need to know whether I can do encoding faster using the library's function
The GMP library provides plenty of bit manipulation primitives. Have a look at the Logical and Bit Manipulation Functions chapter from the documentation.
For your first question, I think you want to use mpz_tstbit. For your second question, it sounds like you're trying to perform some bit shifting, which can be done via the mpz_mul_2exp and the mpz_*_*_2exp functions.

How to allow end user write his own functions?

I'm modeling some financial products and each product has his own pricing formula.
In the application I would like to allow the end-user create his own product with the formula. And this formula can be used by my application to price the product.
Something like :
Formula as string = "f(x) = x * 2"
Dim Result as double = call(Formula, 1)
I know this is possible in Matlab :
f="#(x)(x*2)";
Result = feval(f,1);
I wrote a class in Matlab that implements this feature and integrated it in VB.Net project, but every function takes 4700 times the execution of the same function directly written in VB.Net which is not affordable regarding the business need.
Is that possible in .Net ?
You can look into MEF, so your end users would provide DLL modules in a certain format (see the link I mentioned), which would later be discovered in your program and executed at any given time.
Or use a math parser:
VB.Net- Evaluating Mathematical Expression in a String
Evaluate mathematical expression from a string using VB
But I feel that approach #1 would be more flexible.

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .