Non Trivial Example for Bit Wise shift operators? - logic

My task is: Give one non-trivial example each in which a >> and a >>> operation with identical parameters give the same or different results.
I don't know exactly what to understand by non-trivial example in the context of Bit Wise shift.
The solution i came up with is:
Same result:
00111011 >> 00011101
00111011 >>> 00011101
Different result:
11100110 >> 11110011
11100110 >>> 01110011
However, I don't think this is a non-trivial example but rather trivial, or how exactly do I understand "a non-trivial example"?

Related

How to count 100=(100/24)*24 in bash?

I try to perform this simple calculation, but the programm always gives me incorrect results. If I use only integers, then I get 96
var=$(((100/24)*24))
even if I try to use bc:
var=$(bc <<< "scale=100;100/24*24")
I get something like this:
99.99999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999984;
How to let make the program count the correct way? So that I could get the same values as from the calculator?
This is the result of rounding, to integers (100/24 -> 4 in bash) and floating point (bc lacks a "round" function). See this SO question for related information and links.
It's very hard to provide a general solution here: Most solutions depend on your exact use case, which is probably not what you posted in your question (otherwise you could just put var=100 and move on).
One technique is to reorder the problem (var=$((24*100/24))), which would keep all operations integers.
Another is to multiply by a large offset (var_millionths=$((1000000*(100/24)*24))) and divide out as needed, which would keep enough precision that Bash could round the result well.
Finally, you could (ab)use printf to perform the rounding step for you, because you want the number to be round in base 10 (i.e. when printed):
printf "%.0f\n" $(BC_LINE_LENGTH=0 bc <<< "scale=100;100/24*24")

diophantine analysis in maxima

I have defined an extended Euclidean algorithm in Maxima as
ext_euclid(a,b):=block(
[x,y,d,x_old,y_old,d_old],
if b = 0 then return([1,0,a])
else ([x_old,y_old,d_old]:ext_euclid(b,mod(a,b)),
[x,y,d]:[y_old,x_old-quotient(a,b)*y_old,d_old],
return([x,y,d])));
in order to solve linear Diophantine equations of the form a+b=c where gcd(a,b)=1, however if a-b=c I get -1 returned by the algorithm for the divisor but gcd(a,-b) as before. Is my algorithm wrong, or can it be used for a-b=c?
Iain
I don't quite understand what the problem is. Can you please give two examples, one in which the result matches what you expected, and one in which it doesn't (and please say what's your expected result in that case).
EDIT: OP replies: "to solve 5x+7y is 23 ext_euclid (5,7) returns [3,-2,1] where gcd(5,7) is 1 however for 5x-7y is 23 ext_euclid(5,-7) returns [-3,1,-1] but gcd(5,-7) is still 1 this fails Bezout's identity ax+by is gcd(a,b)"
Also if you are trying to implement a particular statement of the algorithm, can you please link to it or copy it here.
OP replies: "code at http://mvngu.wordpress.com/2009/08/25/elementary-number-theory-using-maxima/"
One possible thing to look at: does the mod function behave as you expect it?
OP replies: "mod(5,7) is, mod(5,-7) is -2"

Writing a program that writes a program

Its well known in theoretical computer science that the "Hello world tester" program is an undecidable problem.(Here is a link what i mean by hello world tester
My question is since given a program as input we can't say what the program will do,can we solve the reverse problem:
Given set of input and output,is there an algorithm for writing a program that writes a program to achieve a one to one mapping between the given input and output.
I know about metaprogramming but my question is more of theoretical interest. Something which can apply for a generic case.
With these kind of musings one has to be very careful. A lot of confusion arises from not clearly distinguishing about a program x for which proposition P(x) holds or any program x for which proposition P(x) hold. As long as the set of programs for which P(x) holds is finite there always is a proof, of their correctness (although this proof may not be known).
At this point you also have to distinguish between programs, which are and can be known and programs which can only be shown to exist by full enumeration of all posibilities. Let's make an example:
Take 10 Programs, which take no input and may or may not terminate and produce "hello World". Then there is a program which decides exactly which of these programs are correct, and which aren't. Lets call these programs (x_1,...,x_10). Then take the programs (X_0,...,X_{2^10}) where X_i output true for program x_j if the j-th bit in the binary representation of i is set. One of these programs has to be the one which decides correctly for all ten x_i, there just might not be any way to ever figure out which one of these 100 X_js is the correct one (a meta-problem at this point).
This goes to show that considering finite sets of programs and input/output pairs one can always resolve to full enumeration and all halting-problem type of paradoxies instantly disappear. In your case the set of generated programs for each input is of size one and the set of input/output pairs is of finite size (because you have to supply it to the meta-program). Hence full enumeration solves your problem very simple and you can also easily proof both the correctness of the corrected program as well as the correctness of the meta-program.
Note: Since the set of generated programs is infinite, this is one of the few cases where you can proof P(x) for a infinite set of programs (actually you can proof P(x,input,output) for this set). This shows that the set being infinite is only a necessary, not a sufficient condition for this type of paradoxes to appear.
Your question is ambiguously phrased.
How would you specify "what a program should do"?
Any precise, complete, and machine-readable specification of a program's functionality is already a program.
Thus, the answer to your question is, a compiler.
Now, you're asking how to find a function based on a sample of its input and output.
That is a question about statistical analysis that I cannot answer.
Sounds like you want to generate a state machine that learns by being given an input sequence and then updates itself to produce the appropriate output sequence. Assuming your output sequences are always the same for the same input sequence it should be simple enough to write. If your output is not deterministic, such as changing the output depending on the time of day, then you cannot automatically generate a state machine.
Depends on what you mean by "one to one mapping". (And also, I suppose, "input" and "output".)
My guess is that you're asking whether, given an example of inputs and outputs for a given arbitrary program, can one devise an algorithm to write an equivalent program? If so, the answer is no. Eg, you could have a program with the inputs/outputs of 1/1, 2/2, 3/3, 4/4, and yet, if the next input value was 5, the output would be 3782. There's no way to know, from a given set of results, what the next result might be.
The question is underspecified since you did not say how the input and output are presented. For finite lists, the answer is "yes", as in this Python code:
def f(input,output):
print "def g():"
print " x = {" + ",".join(repr(x) + ":" + repr(y) for x,y in zip(input,output)) + "}"
print " print x[raw_input()]"
>>> f(['2','3','4'],['a','b','x'])
def g():
x = {'2':'a','3':'b','4':'x'}
print x[raw_input()]
>>> def g():
... x = {'2':'a','3':'b','4':'x'}
... print x[raw_input()]
...
>>> g()
3
b
for infinite sets how are you going to present them? If you show only a small sample of input this does not specify the whole algorithm. Guessing the best fit is undecidable. If you have a "magic blackbox" then there are continuum many mappings but only a countable number of programs, so it's impossible.
I think I agree with SLaks, but taking things from a different angle, what does a compiler do?
(EDIT: I see SLaks edited his original answer, which was essentially 'you're describing the identity function').
It takes a program in one source language that describes the intended behaviour of a program, and "writes" another program in a target language that exhibits that behaviour.
We could also think of this in terms of things like process refinement --- given an abstract specification, we can construct a refinement mapping to some "more concrete" (read: less non-deterministic, usually) implementation.
But based on your question, it's really very difficult to tell which of these you meant, if any.

Numeric instability

I'm doing some Linear programming exercises for the course of Algorithms, and in doing this I'm solving manually many operations with fractions. In doing this I realized that a human being don't suffer from numeric instability: we just keep values in fractional representation, and we finally evaluate (possibly by using a calculator) the value of expressions.
Is there any technique that does this automatically?
Im thinking of something which achieves some kind of symbolic computation, simplifies the numbers internally and finally yields the value only during the evaluation of an expression.
Boost contains a rational number library here which might be of help.
In Python you can have a look at fractions:
import fractions
a = fractions.Fraction(2,3)
a*2
# Fraction(4, 3)
a**2
# Fraction(4, 9)
'Value: %.2f' % a
# 'Value: 0.67'

MATLAB script to generate reports of rounding errors in algorithms

I am interested in use or created an script to get error rounding reports in algorithms.
I hope the script or something similar is already done...
I think this would be usefull for digital electronic system design because sometimes it´s neccesary to study how would be the accuracy error depending of the number of decimal places that are considered in the design.
This script would work with 3 elements, the algorithm code, the input, and the output.
This script would show the error line by line of the algorithm code.
It would modify the algorith code with some command like roundn and compare the error of the output.
I would define the error as
Errorrounding = Output(without rounding) - Output round
For instance I have the next algorithm
calculation1 = input*constan1 + constan2 %line 1 of the algorithm
output = exp(calculation1) %line 2 of the algorithm
Where 'input' is the input of n elements vector and 'output' is the output and 'constan1' and 'constan2' are constants.
n is the number of elements of the input vector
So, I would put my algorithm in the script and it generated in a automatic way the next algorithm:
input_round = roundn(input,-1*mdec)
calculation1 = input*constant1+constant2*ones(1,n)
calculation1_round = roundn(calculation1,-1*mdec)
output=exp(calculation1_round)
output_round= roundn(output,-1*mdec)
where mdec is the number of decimal places to consider.
Finally the script give the next message
The rounding error at line 1 is #Errorrounding_calculation1
Where '#Errorrounding' would be the result of the next operation Errorrounding_calculation1 = calculation1 - calculation1_round
The rounding error at line 2 is #Errorrounding_output
Where 'Errorrounding_output' would be the result of the next operation Errorrounding_output = output - output_round
Does anyone know if there is something similar already done, or Matlab provides a solution to deal with some issues related?
Thank you.
First point: I suggest reading What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg. It should illuminate a lot of issues regarding floating-point computations that will help you understand more of the intricacies of the problem you are considering.
Second point: I think the problem you are considering is a lot more complicated than you realize. You are interested in the error introduced into a calculation due to the reduced precision from rounding. What you don't realize is that these errors will propagate through your computations. Consider your example:
output = input*C1 + C2
If each of the three operands is a double-precision floating-point number, they will each have some round-off error in their precision. A bound on this round-off error can be found using the function EPS, which tells you the distance from one double-precision number to the next largest one. For example, a bound on the relative error of the representation of input will be 0.5*eps(input), or halfway between it and the next largest double-precision number. We can therefore estimate some errors bounds on the three operands as follows:
err_input = 0.5.*eps(input); %# Maximum round-off error for input
err_C1 = 0.5.*eps(C1); %# Maximum round-off error for C1
err_C2 = 0.5.*eps(C2); %# Maximum round-off error for C2
Note that these errors could be positive or negative, since the true number may have been rounded up or down to represent it as a double-precision value. Now, notice what happens when we estimate the true value of the operands before they were rounded-off by adding these errors to them, then perform the calculation for output:
output = (input+err_input)*(C1+err_C1) + C2+err_C2
%# ...and after reordering terms
output = input*C1 + C2 + err_input*C1 + err_C1*input + err_input*err_C1 + err_C2
%# ^-----------^ ^-----------------------------------------------------^
%# | |
%# rounded computation difference
You can see from this that the precision round-off of the three operands before performing the calculation could change the output we get by as much as difference. In addition, there will be another source of round-off error when the value output is rounded off to represent it as a double-precision value.
So, you can see how it's quite a bit more complicated than you thought to adequately estimate the errors introduced by precision round-off.
This is more of an extended comment than an answer:
I'm voting to close this on the grounds that it isn't a well-formed question. It sort of expresses a hope or wish that there exists some type of program which would be interesting or useful to you. I suggest that you revise the question to, well, to be a question.
You propose to write a Matlab program to analyse the numerical errors in other Matlab programs. I would not use Matlab for this. I'd probably use Mathematica, which offers more sophisticated structural operations on strings (such as program source text), symbolic computation, and arbitrary precision arithmetic. One of the limitations of Matlab for what you propose is that Matlab, like all other computer implementations of real arithmetic, suffers rounding errors. There are other languages which you might choose too.
What you propose is quite difficult, and would probably require a longer answer than most SOers, including this one, would be happy to contemplate writing. Happily for you, other people have written books on the subject, I suggest you start with this one by NJ Higham. You might also want to investigate matters such as interval arithmetic.
Good luck.

Resources