tags:

views:

779

answers:

8

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;?

A: 

You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.

CiNN
+6  A: 

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.

Simon
I'm not sure why you made the comment about using an OO language. It would be possible to make a library in just about any language to handle indeterminate variable types.
Trent
A: 

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).

flolo
A: 

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 .

Geo
man, that's pretty cool! I think I want to check out Perl!
Ziggy
+1  A: 

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.

Cruachan
+1  A: 

Damien Conways Quantum::Superpositions might do what you want,

http://search.cpan.org/~dconway/Quantum-Superpositions-1.03/lib/Quantum/Superpositions.pm

You might need your crack-pipe however.

Kent Fredric
ha ha ha yeah, this is what I'm talking about.
Ziggy
+5  A: 

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 experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).

Andrey Shchekin
sweet! This... Perl, she sounds pretty fine.
Ziggy
(1|2|3)+1=(1+1|2+1|3+1)=(2|4|5)? I think you have an error in there. :o)
Michael Madsen
Michael, thanks :). Fixed.
Andrey Shchekin
+2  A: 

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 ...

Guillaume