How to recursively determine quotient in prolog with limited predicates? - recursion

So I have been trying to determine a quotient of integers in prolog by only using build in predicates "is", "*", "+" and "-". It expects the quotient to be an integer so I don't need any decimals or rest.
I quickly came up with a solution but for some reason it just won't work as intended. It keeps giving me the same error and I just can't figure out what I am doing wrong. I am new to prolog btw. and due to university I have to look into it.
Here is what I am trying:
div(0, 0, _).
div(1, Dividend, Dividend).
div(Quotient, Dividend, Divisor) :-
NewDividend is Dividend - Divisor,
div(NewQuotient, NewDividend , Divisor),
NewQuotient + 1 is Quotient.
I have basically tried anything from swapping stuff around to adding more predicates such as "div(Dividend, Dividend, 1).".
Here is what I shows when I type div(Y, 15, 3).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [13] 0+1 is _2768
ERROR: [12] div(_2798,3,3) at div.pl:63
ERROR: [11] div(_2826,6,3) at div.pl:63
ERROR: [10] div(_2854,9,3) at div.pl:63
ERROR: [9] div(_2882,12,3) at div.pl:63
ERROR: [8] div(_2910,15,3) at div.pl:63
ERROR: [7] <user>
I kinda understand what the errors want to tell me but I just don't get why it won't work as I expect it to work. It gets that the quotient is 0 if dividend is 0 and from there on it should just add 1s until it is back at the first call but it won't. I have to use this signature for my predicate by the way. It is an assignment from the university.
Any help is appreciated!

I found the solution myself:
div(0,0,_).
div(Quotient, Dividend, Divisor) :-
Rest is Dividend - Divisor,
div(NewQuotient, Rest, Divisor),
Quotient is NewQuotient + 1.

Related

Why is the FOR loop in my program producing empty matrices?

I am having a problem running a spiking-neuron simulator. I keep getting the error message, "operation +: Warning adding a matrix with the empty matrix will give an empty matrix result." Now I'm writing this program in "Scilab," but I'm hoping the problem I am having will be clear for the educated eye regardess. What I am doing is converting an existing MATLAB program to Scilab. The original MATLAB program and an explanation can be found here: https://www.izhikevich.org/publications/spikes.pdf
What happens in my Scilab version is that the first pass through the loop produces all the expected values. I Know this becuase I hit pause at the end of the first run, right before "end," and check all the values and matrix elements. However, if I run the program proper, which includes a loop of 20 iterations, I get the error message above, and all of the matrix values are empty! I cannot figure out what the problem is. I am fairly new to programming so the answer may be very simple as far as I know. Here is the Scilab version of the program:
Ne=8; Ni=2;
re=rand(Ne,1); ri=rand(Ni,1);
a=[0.02*ones(Ne,1); 0.02+0.08*ri];
b=[0.2*ones(Ne,1); 0.25-0.05*ri];
c=[-65+15*re.^2; -65*ones(Ni,1)];
d=[8-6*re.^2; 2*ones(Ni,1)];
S=[0.5*rand(Ne+Ni,Ne), -rand(Ne+Ni,Ni)];
v=60*rand(10,1)
v2=v
u=b.*v;
firings=[];
for t=1:20
I=[5*rand(Ne,1,"normal");2*rand(Ni,1,"normal")];
fired=find(v>=30);
j = length(fired);
h = t*ones(j,1);
k=[h,fired'];
firings=[firings;k];
v(fired)=c(fired);
u(fired)=u(fired)+d(fired);
I=I+sum(S(:,fired),"c");
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
u=u+a.*(b.*v-u);
end
plot(firings(:,1), firings(:,2),".");
I tried everything to no avail. The program should run through 20 iterations and produce a "raster plot" of dots representing the fired neurons at each of the 20 time steps.
You can add the following line
oldEmptyBehaviour("on")
at the beginning of your script in order to prevent the default Scilab rule (any algebraic operation with an empty matrix yields an empty matrix). However you will still have some warnings (despite the result will be OK). As a definitive fix I recommend testing the emptyness of fired in your code, like this:
Ne=8; Ni=2;
re=rand(Ne,1); ri=rand(Ni,1);
a=[0.02*ones(Ne,1); 0.02+0.08*ri];
b=[0.2*ones(Ne,1); 0.25-0.05*ri];
c=[-65+15*re.^2; -65*ones(Ni,1)];
d=[8-6*re.^2; 2*ones(Ni,1)];
S=[0.5*rand(Ne+Ni,Ne), -rand(Ne+Ni,Ni)];
v=60*rand(10,1)
v2=v
u=b.*v;
firings=[];
for t=1:20
I=[5*rand(Ne,1,"normal");2*rand(Ni,1,"normal")];
fired=find(v>=30);
if ~isempty(fired)
j = length(fired);
h = t*ones(j,1);
k=[h,fired'];
firings=[firings;k];
v(fired)=c(fired);
u(fired)=u(fired)+d(fired);
I=I+sum(S(:,fired),"c");
end
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
u=u+a.*(b.*v-u);
end
plot(firings(:,1), firings(:,2),".");
The [] + 1 is not really defined in a mathematical sense. The operation might fail or produce different results depending on the software you use. For example:
Scilab 5 [] + 1 produces 1
Scilab 6 [] + 1 produces [] and a warning
Julia 1.8 [] .+ 1 produces [] but [] + 1 an error.
Python+Numpy 1.23 np.zeros((0,0)) + 1 produces [].
I suggest checking with size() or a comparison to the empty matrix to avoid such strange behaviour.

LoadError: UndefVarError in julia for loop

I am defining a variable phi_old at the start of my function, and assigning it a value of phi_start.
I then have an iterative loop that:
uses phi_old
solves a PDE to get a solution T
uses the solution T to calculate phi_new
checks the difference between phi_new and phi_old, if it is larger than error the iterative loop begins again, with phi_old using the values of phi_new.
if the difference is less than the error, the iteration stops.
But I am getting this error:
Warning: Assignment to `T` in soft scope is ambiguous because a global variable by the same name exists:
ERROR: LoadError: UndefVarError: phi_old not defined
Here is an example of my code's structure:
phi_old = createCellVariable(m,phi_start)
for i=1:i_end
phi_new = myfunction(T)
error = sum(phi_new.value[2,1:end]-phi_old.value[2,1:end])
if error > 1E-03
phi_old = phi_new
else
i=i+1
break
end
end
Are there any techniques I could use to better initialize/assign my arrays and fix this error? The arrays are quite large, so I would like to preallocate if possible, and only copy arrays if necessary.

Julia: Even-number datatype for functions

I have about 50 functions which should consume only even positive numbers. Right now I am checking each time with an "if" whether the number put in is zero or not:
function grof(x::Int)
if (x % 2) == 0
println("good")
else
throw("x is not an even number!!!!!!!!!!!!! Stupid programmer!")
end
end
Ideally, I would like to have a datatype which produces this automatically, i.e.
function grof(x::EvenInt)
println("good")
end
However, I am not able to produce this datatype by my own since I am unable to understand the documentary. Thanks for your help!
Best, v.
I don't think creating a type is warranted in such a situation: I would simply #assert that the condition is verified at the beginning of the function(s). (Funnily enough, checking the whether a number is even is the example that was chosen in the documentation to illustrate the effect of #assert)
For example:
julia> function grof(x::Int)
#assert iseven(x) "Stupid programmer!"
println("good")
end
grof (generic function with 1 method)
julia> grof(2)
good
julia> grof(3)
ERROR: AssertionError: Stupid programmer!
Stacktrace:
[1] grof(::Int64) at ./REPL[5]:2
[2] top-level scope at REPL[7]:1
EDIT: If you really want to create a type enforcing such a constraint, it is possible. The way to do this would be to
create a type (possibly subtyping one of the Number abstract types; maybe Signed)
define an inner constructor ensuring that such a type cannot hold an odd value
A very simple example to build upon would be along the lines of:
# A wrapper around an even integer value
struct EvenInt
val :: Int
# inner constructor
function EvenInt(val)
#assert iseven(val)
new(val)
end
end
# Accessor to the value of an EvenInt
val(x::EvenInt) = x.val
# A method working only on even numbers
grof(x::EvenInt) = println("good: $(val(x)) is even")
You'd use this like so:
julia> x = EvenInt(42)
EvenInt(42)
julia> grof(x)
good: 42 is even
julia> y = EvenInt(1)
ERROR: AssertionError: iseven(val)
Stacktrace:
[1] EvenInt(::Int64) at ./REPL[1]:5
[2] top-level scope at REPL[6]:1
but note that you can't do anything on EvenInts yet: you need to either unwrap them (using val() in this case), or define operations on them (a task which can be vastly simplified if you make EvenInt a subtype of one of the abstract number types and follow the relevant interface).
All integers multiplied by two are even, so redefine your function to take half the number it currently takes.
function grof2(halfx::Int)
x=2*halfx
println("good")
end

no method matching in subtraction

I'm using julia 0.5 after run this code :
Freqsample = 100;
second = 4;
step = (Freqsample * second )-1
i get this Error :
MethodError: no method matching getindex(::Int64, ::Colon, ::UnitRange{Int64})
in -(::Int64, ::Int64) at main.jl:12
in include_string(::String, ::String) at loading.jl:441
in eval(::Module, ::Any) at boot.jl:234
in (::Atom.##65#68)() at eval.jl:40
in withpath(::Atom.##65#68, ::Void) at utils.jl:30
in withpath(::Function, ::Void) at eval.jl:46
in macro expansion at eval.jl:109 [inlined]
in (::Atom.##64#67{Dict{String,Any}})() at task.jl:60
Whats wrong with subtracting ? i'm pretty new to julia forgive if its a dumb question
You've redefined - for more types than you probably intended. The second line in the backtrace you posted is telling you that Julia called a - method in main.jl for two integers. And line one is saying that within there it's trying to do something like x[:, 1:5] at line 12, where x is an integer.
This tells me two things;
Your definition of - is probably typed too permissively. You probably didn't intend to accept integers.
You are probably shadowing the built in - definition instead of extending it. You need to import Base: - in order to add a new method to a function in the standard library.

Appending to Vector

I'm having trouble appending to an empty vector in Julia.
v = Int64[]
append!(v,1)
append(v,1)
The append! gives the error
ERROR: `Variable` has no method matching Variable(::Int64, ::Int64, ::Int64, ::Int64)
And append gives the error
ERROR: append not defined
This is probably a basic mistake on my part, but I can't figure out why neither command is working.
If you're appending a scalar value, you want push!. If you're adding a list of elements, then you want append!. There's a good reason for the distinction, as you will probably realize if you consider what should happen if you want to build an array-of-arrays.
Typing ?append! at the REPL will show you help on the function, including a demo on how to use it. (In julia 0.4, the help has been improved and refers you to the push! function as well, but that doesn't seem to have been implemented in the current release.)

Resources