Julia - trying to compare two strings - julia

I am trying to compare two strings I got as input, but it printed at me an error like "syntax: unexpected "="".
x = readline()
y = readline()
y = println(cmp(y, "Ciao"))
x = println(cmp(x, "Ciao"))
if x=1
println("Ho fatto un confronto in modo giusto")
elseif x=0
println("No simone, non hai inserito quello che mi aspettavo")
elseif y=1
println("Hai inserito la y e l'ho controllata, il programma funziona")
elseif y=0
println("No simone, non hai inserito quello che mi aspettavo")
else println("Il programma non ha funzionato")
end
return x,y

In Julia, you should use == for comparison: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons
This is different from assignment operator =, so for example
# Assignment
julia> x = 1
1
# Comparison
julia> x == 1
true
Also: println returns nothing so this code still wouldn’t work even with that fix. If you delete the calls to println and fix the assignments to be equality checks, then the code might work.

Related

In Julia, a value accessed from a matrix failed to be used as an argument in a function

First, I have a function called permeability.
# permeabiliy function
# L is short for the Lable
mu_0 = 4 * pi * 10^(-7);
mu_r_core = 50;
mu_r_air = 1;
L = Int16;
function permeability(L)
if L in 1:4
if L !== 3
return mu = mu_r_air * mu_0
else
return mu = mu_r_core * mu_0
end
else
println("null") #print output in a new line
end
end
Then, I have a matrix called domain, which is shown below,
domain
2392-element Array{Int16,1}:
1
1
3
1
...
When I called permeability(domain[3]), the output is,
L = domain[3]
permeability(L)
the output is
1.2566370614359177e-6
However, when I simply called permeability(3),
permeability(3)
the output is
6.283185307179588e-5
So, it seems that the value passed from matrix domain is just "1", but in this case, domain[3] should be 3 and the result should be the same in these 2 cases.
Can someone please tell me where I was wrong?
The problem is that your array stores Int16 while 3 is an Int64. L!==3 requires L to be of the same type as 3 ie Int64. You wanted instead L!=3. Your confusion probably comes from the fact that != is the inverse of == while !== is the inverse of ===

Can anyone simplify a && b && ( True || c ) to 1?

I found this in a coursebook and think there might be an error in it since I cannot see how it simplifies to 1, I get AB instead...
being a de morgans law here we consider A=B=C=1 and so A'=B'=C'=0 (A' is A rise to dash)
so L.H.S last statement is = A.B(1+C) = 1.1(1+1) = 1.1 =1
HERE 1.1 =1 and 1+1 =1 (applying the demorgans law) you may google the use formula

IDL two step graph

I'm struggling with setting a y(x) condition that varies with x range. As an example below, the code wants to plot y=x between x=0 and x=5.1; otherwise y=2x.
Upon compilation, the code spits out the following: Expression must be a scalar or 1 element array in this context:
In other words don't know how to assign an array variable 'x' into if statement.
Thank you all for your help in advance.
PRO test
x = findgen(101.0,start=0)/10.0 ; 0.0 start, 10.0 end increment of 0.1
print,x
if x lt 5.1 then begin
y = 1.0 * x ;
endif else begin
y = 2.0* x
endelse
graph1=plot(x,y,thick=2,NAME=first,/CURRENT, $
linestyle = 0, ytitle=' y',xtitle='x' ) ; O
END
The problem is the test in your IF statement. Use WHERE instead to do something like the following.
y = x ;; need to initialize variable
low = WHERE(x lt 5.1,lw,COMPLEMENT=upp,NCOMPLEMENT=up)
IF (lw[0] GT 0) THEN y[low] = x[low] ;; technically don't need this line
IF (up[0] GT 0) THEN y[upp] = 2e0*x[upp]

Using method source(edge) of Package Graphs.jl in Julia on Juliabox.org

Take a look at the following simple code example:
Pkg.add("Graphs")
using Graphs
gd = simple_graph(20, is_directed=true) # directed graph with 20 nodes
nodeTo = 2
for nodeFrom in vertices(gd) # add some edges...
if(nodeTo != 20)
add_edge!(gd, nodeFrom, nodeTo)
nodeTo +=1
end
end
for i in edges(gd) # Print source and target for every edge in gd
println("Target: ",target(i))
println("Source: ", source(i))
end
So it works sometimes, and it prints the source and targets of the edges, but most times running the cell(after programming in this or other cells or doing nothing) i get the following error:
type: anonymous: in apply, expected Function, got Int64
while loading In[11], in expression starting on line 14
in anonymous at no file:16
I have not change any code concerning the method nether the cell, but it doesnt work anymore. The method target(edge) works fine, but the method source(edge) makes problems the most times.
http://graphsjl-docs.readthedocs.org/en/latest/graphs.html#graph
What should i do? I would be pleased to get some help.
After some thoughts, i found out, that the mistake have to be in the code between the hashtags:
Pkg.add("JuMP")
Pkg.add("Cbc")
# Pkg.update()
using Graphs
using JuMP
using Cbc
function createModel(graph, costs, realConnections)
m = Model(solver = CbcSolver())
#defVar(m, 0 <= x[i=1:realConnections] <= 1, Int)
#setObjective(m, Min, dot(x,costs[i=1:realConnections]))
println(m)
for vertex in vertices(graph)
edgesIn = Int64[] # Array of edges going in the vertex
edgesOut = Int64[] # Array of Edges going out of the vertex
for edge in edges(graph)
if (target(edge) == vertex) # works fine
push!(edgesIn, edge_index(edge))
end
if (source(edge) == vertex) # does not work
push!(edgesOut, edge_index(edge))
print(source(edge), " ")
end
end
# #addConstraint(m, sum{x[edgesIn[i]], i=1:length(edgesIn)} - sum{x[edgesOut[j]], j=1:length(edgesOut)} == 0)
end
return m
end
file = open("csp50.txt")
g = createGraph(file) # g = g[1] = simpleGraph Object with 50 nodes and 173 arccs, g[2] = number of Nodes g[3]/g[4] = start/end time g[5] = costs of each arc
# After running this piece of code, the source(edge) method does not work anymore
########################################################################################
# adding a source and sink node and adding edges between every node of the orginal graph and the source and sink node
realConnections = length(g[5]) # speichern der Kanten
source = (num_vertices(g[1])+1)
sink = (num_vertices(g[1])+2)
add_vertex!(g[1], source)
add_vertex!(g[1], sink)
push!(g[3], 0)
push!(g[3], 0)
push!(g[4], 0)
push!(g[4], 0)
for i in vertices(g[1])
if (i != source)
add_edge!(g[1], source, i) # edge from source to i
push!(g[5], 0)
end
if (i != sink)
add_edge!(g[1], i, sink) # Kante von i zu Senke
push!(g[5], 0) # Keine Kosten/Zeit fuer diese Kante
end
end
######################################################################################
numEdges = num_edges(g[1]);
createModel(g[1], g[5], realConnections)
From Julia's Manual:
Julia will even let you redefine built-in constants and functions if
needed:
julia> pi
π = 3.1415926535897...
julia> pi = 3
Warning: imported binding for pi overwritten in module Main
3
julia> pi
3
julia> sqrt(100)
10.0
julia> sqrt = 4
Warning: imported binding for sqrt overwritten in module Main
4
However, this is obviously not recommended to avoid potential
confusion.
So reusing source as a variable "unbound" it from it's function definition. Using a different variable name should preserve the Graphs.jl definition for it.

Jacobi method in Scilab, what is wrong with my implementation

Hy,
I have the below Jacobi method implementation in Scilab, but I receaive errors,
function [x]= Jacobi(A,b)
[n m] = size (A); // determinam marimea matricei A
//we check if the matrix is quadratic
if n<>m then
error('Matricea ar trebui sa fie patratica');
abort;
end
we initialize the zeros matrix
x = zeros(n,1) // matrice cu zerouri x = [0, 0 ... 0]
// initializarea variabilelor
Itmax=1000.; //numar maxim de iteratii
eps=0.0000000000000001 // toleranta maxima
nrit=0;
ready=0;
while (ready==0)
for i=1:n
s=0
for j=1:n
if i<>j then
s=A(i,j)+x(j);
end
end
y(i)=(1/A(i,i))*(b(i)-s);
end
abso = norm(x(1),y(1));
for i=2:n
if abso<norm(x(i),y(i)) then
abso=norm(x(i),y(i));
end
end
nrit=nrit+1;
if((nrit==Itmax)|(abso<eps)) then
ready=1;
end
x=y;
end
for i=1:n
disp(y(i),'Rezidurile medii');
end
x
What is wrong with it!
Sincerly,
I don't know what errors you get and I noticed there are some issues in the implementation as well, but you can use the following code which works fine:
Jacobi Alghoritm
function [x]= Jacobi(A,b)
[n m] = size (A); // determinam marimea matricei A
if n<>m then
error('Matricea ar trebui sa fie patratica');
end
// initializarea variabilelor
x = zeros(n,1) // matrice cu zerouri x = [0, 0 ... 0]
Itmax=1000.; //numar maxim de iteratii
eps=0.0000000000000001 // toleranta maxima
nrit=0;
ready=0;
while (ready==0)
for i=1:n
s=0
for j=1:n
if i<>j then
s=s+A(i,j)*x(j);
end
end
y(i)=(1/A(i,i))*(b(i)-s);
end
abso = abs(x(1)-y(1));
for i=2:n
if abso<abs(x(i)-y(i)) then
abso=abs(x(i)-y(i));
end
end
nrit=nrit+1;
if((nrit==Itmax)|(abso<eps)) then
ready=1;
end
x=y;
end
Let's say you saved this in a file at ~/Jacobi.sci
Usage (in Scilab)
-->exec('~/Jacobi.sci', -1)
-->Jacobi([3,-1,-1;-1,3,1;2,1,4],[1,3,7])
ans =
1.
1.
1.

Resources