qualifying confilicting functions in Julia - julia

I have following conflicting exports:
using DataFrames
using DataStructures
Following returns an error:
tail(dfc)
WARNING: both DataStructures and DataFrames export "tail"; uses of it in module Main must be qualified
ERROR: UndefVarError: tail not defined
I saw this syntax on one forum, but it still fails:
DataFrames::tail(dfc)
ERROR: UndefVarError: tail not defined
Any ideas?

This appears to work for julia version 0.4.6:
julia> using DataFrames, DataStructures
julia> lst = list(1, 2, 3)
list(1, 2, 3)
julia> DataStructures.tail(lst)
list(2, 3)
The :: is used for specifying type information.

Related

Julia DiGraph with user-defined object as nodes

In Julia, I can easily create a graph using the very nicely written Graphs.jl library:
julia> using Graphs
julia> g = SimpleDiGraph(2)
{2, 0} directed simple Int64 graph
julia> add_edge!(g, 1,2)
true
julia> add_edge!(g, 2, 1)
true
However, I cannot seem to make the nodes anything other than integers. To wit, I would like to do something like this:
julia> using Graphs
julia> abstract type Foo end
julia> s = SimpleDiGraph(typeof(Foo)) # doesn't work.
julia> mutable struct Bar <: Foo
b::Bool
end
julia> mutable struct Baz <: Foo
b::Bool
end
julia> bar = Bar(true)
Bar(true)
julia> baz = Baz(false)
Baz(false)
julia> add_edge!(g, bar, baz) # Again, doesn't work.
How can I make the nodes a custom type?
Note: I have tried the following workaround, as it appears that only integral types are allowed in the nodes:
julia> add_edge!(g, Int64(pointer_from_objref(bar)), Int64(pointer_from_objref(baz)))
but this one, although not throwing an exception, returns false.
In the Graphs.jl tutorial, it is stated that "integers, and only integers, may be used for describing vertices." So my first goal is out of the question (although it may be possible in, say, MetaGraphs.jl). The recommendation is to use the integer value in the vertex as an index into a vector and store the objects in the vector.
Moreover, as it is stated that the goal is to index into a vector, the integers must be contiguous, and hence my second idea of using the address of the object is also not feasible.
However, using the graph to index into a vector is a perfectly acceptable solution for me.

How to use ModelingToolkit.jl to create variables dynamically

The main problem is that my variables would be determined only after running the codes (because the number of variables is not fixed).
In old version ModelingToolkit.jl, I used the following codes to generate a variable.
my_var = Variable(Symbol(name))(t) # name is a string
However, it can’t work in the latest version. Here is the error.
ERROR: Sym name is not callable. Use #syms name(var1, var2,...) to create it as a callable.
I've checked SymbolicUtils.jl but didn't find other usages. How can I fix this problem?
You can use the #variables macro to create symbolic variables at runtime as well. The $ operator interpolates the runtime value.
julia> using ModelingToolkit
julia> z = :abc;
julia> k = #variables $z
1-element Vector{Num}:
abc
julia> k[1]
abc

Understanding the step cannot be zero error in Julia Language?

I need a vector of UnitRanges as follows:
[2:5, 3:6, 4:7, 5:8]
when I try to run this (2:5):(5:8), I get an error of "ArgumentError: step cannot be zero."
Is there a way of creating a UnitRange array using UnitRange itself?
It sounds like you want to map or broadcast : over the elements in the two arguments. Just do it explicitly:
julia> map(:, 2:5, 5:8)
4-element Array{UnitRange{Int64},1}:
2:5
3:6
4:7
5:8
Now, ideally you'd also be able to write this as (2:5) .: (5:8) — you'd dot the : operator to broadcast it — but since : is used for so many things and since this isn't a very common use-case we've not enabled the dotting of :. You can, however, use the non-infix form and dot that:
julia> (:).(2:5, 5:8)
4-element Array{UnitRange{Int64},1}:
2:5
3:6
4:7
5:8
As for the error message you're getting, it's because the first thing : tries to do is determine the length, assuming its two arguments are scalars. To do this it subtracts the first argument from the second:
julia> (5:8) - (2:5)
ERROR: ArgumentError: step cannot be zero
That fails because it's trying to create a step range that's effectively 3:0:3 and has a length of 4.

Choosing tuples from a list that satisfy a condition in SAGE

I would like to create a program that chooses certain tuples from a list of tuples that satisfy certain conditions.
Here's the basic case of what I am trying to do:
First I create a list of length 3 tuples with coordinates in the set [-1,1]:
Tupes=Tuples([-1,1],3)
And now I am trying to extract those members whose coordinates sum up to 1. I have tried running
[Tupes[k] for k in len(Tupes) if sum([Tupes[k][j] for j in range(len(Tupes[k]))])==1]
but am getting the error
Error in lines 1-1
Traceback (most recent call last):
File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 995, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
TypeError: 'int' object is not iterable
I am new to SAGE and python, so this is just an exercise I am trying to do to learn.
Your immediate problem is that you are going in len(Tupes) which is just a number, rather than in range(len(Tupes)) which would be a list. You can't iterate a number, only a list.
That said, there are some other things you can do to make this tighter. Here is one suggestion.
Tupes=Tuples([-1,1],3)
[Tupes[k] for k in range(Tupes.cardinality()) if sum(Tupes[k])==1]
Somewhat simpler than kcrisman's answer:
sage: Tupes=Tuples([-1,1],3)
sage: [k for k in Tupes if sum(k) == 1]
[[1, 1, -1], [1, -1, 1], [-1, 1, 1]]

Can't declare number type in Julia

According to http://julia.readthedocs.org/en/latest/manual/integers-and-floating-point-numbers/, one should be able to do this:
julia> Float32(-1.5)
-1.5f0
Instead, I get:
julia> Float32(-1.5)
ERROR: type cannot be constructed
This happens for all other attempts to use this syntax, e.g. x = Int16(1)
I'm on 0.3.10.
You are on 0.3.10 but reading the manual for 0.5. In the manual for 0.3 http://julia.readthedocs.org/en/release-0.3/manual/integers-and-floating-point-numbers/
Values can be converted to Float32 easily:
julia> float32(-1.5)
-1.5f0
julia> typeof(ans)
Float32

Resources