Problem
I want to use sbt to execute custom shell scripts (as a part of a bigger build) within multiple directories in an order that respects a defined dependency graph. For example, given directories A, B, C and D and dependencies B after A, C after B, D after B the following is correct:
A, B, C, D
A, B, D, C
and the following is not correct:
B, A, C, D
A, C, B, D etc.
I created a custom key and custom task.
val script = taskKey[String]("script")
script := println("script")
and defined 4 projects:
lazy val a = Project(id = "a", base = file("A"))
lazy val b = Project(id = "b", base = file("B")).dependsOn(a)
lazy val c = Project(id = "c", base = file("C")).dependsOn(b)
lazy val d = Project(id = "d", base = file("D")).dependsOn(b)
I'm trying to run "script" task in SBT Console and it produces a random execution order that does not respect dependencies above.
Question
Does dependsOn mean a generic dependency or some Java/Scope/Whatever specific dependency?
Is there a solution at all and if yes then what is it?
Related
I have a model with many parameters where I am passing them as a named tuple. Is there a way to promote the values into the variable scope in my function?
parameters = (
τ₁ = 0.035,
β₁ = 0.00509,
θ = 1,
τ₂ = 0.01,
β₂ = 0.02685,
...
)
And then used like so currently:
function model(init,params) # params would be the parameters above
foo = params.β₁ ^ params.θ
end
Is there a way (marco?) to get the parameters into my variable scope directly so that I can do this:
function model(init,params) # params would be the parameters above
#promote params # hypothetical macro to bring each named tuple field into scope
foo = β₁ ^ θ
end
The latter looks a lot nicer with some math-heavy code.
You can use #unpack from the UnPack.jl package1:
julia> nt = (a = 1, b = 2, c = 3);
julia> #unpack a, c = nt; # selectively unpack a and c
julia> a
1
julia> c
3
1 This was formerly part of the Parameters.jl package, which still exports #unpack and has other similar functionality you might find useful.
Edit: As noted in the comments, writing a general macro #unpack x is not possible since the fieldnames are runtime information. You could however define a macro specific to your own type/namedtuple that unpacks
julia> macro myunpack(x)
return esc(quote
a = $(x).a
b = $(x).b
c = $(x).c
nothing
end)
end;
julia> nt = (a = 1, b = 2, c = 3);
julia> #myunpack nt
julia> a, b, c
(1, 2, 3)
However, I think it is more clear to use the #unpack since this version "hides" assignments and it is not clear where the variables a, b and c comes from when reading the code.
So I am writing a database that contains the rule waywest(X,Y). waywest takes 2 buildings that are along a street and returns where or not the building X is more than one building west of building Y. I have:
waywest(X,Y) :- not(west(X,Y)).
waywest(X,Y) :- not(west(X,Z)) , waywest(Z,Y).
This is a method that recursively loops through using the fact west(X,Y), where building X is west of building Y. I keep getting this existence error and debugging doesn't work either.
The way you programmed it, you can only use it to disprove facts, according to the documentation of not in swi-prolog:
not(:Goal)
True if Goal cannot be proven. Retained for compatibility only. New code should use +/1.
Moreover I would discourage the use of NOT at all at the beginning of Prolog, because it's not (sic!) working as you might intend/expect in the beginning.
If I understood you problem correctly, this should also do the trick:
west(a,b).
west(b,c).
west(c,d).
west(d,e).
waywest(X,Z) :-
west(X,Y),
west(Y,Z).
waywest(X,Z) :-
west(X,Y),
waywest(Y,Z).
If we now check it we get as expected:
?- waywest(X,Y).
X = a, Y = c ;
X = b, Y = d ;
X = c, Y = e ;
X = a, Y = d ;
X = a, Y = e ;
X = b, Y = e ;
false.
I want to create a package in R that uses cpp-code with the Rcpp package. In the cpp-code I need to use classes and member functions with named arguments.
After building the package, functions inside of Rcpp-modules seem to be ignoring named inputs and parse by position instead.
For example using the test package, as described below, I get the following output
# test the library
library(test)
# foo1 is a function outside of a module
foo1(a = "a", b = "b") # works as expected
#> a is a, b is b
foo1(b = "b", a = "a") # works as expected
#> a is a, b is b
# function within module "ABCMODULE"
foo2(a = "a", b = "b") # works as expected
#> a is a; b is b
foo2(b = "b", a = "a") # switches arguments <-------
#> a is b; b is a
abc <- new(ABC)
abc$foo3(a = "a", b = "b") # works as expected
#> a is a; b is b
abc$foo3(b = "b", a = "a") # switches arguments <-------
#> a is b; b is a
I have looked into the many Rcpp-vignettes but could find any solution to this. Have I overlooked something in the documents? Did I do a mistake somewhere? And how can I solve this or is this a feature/bug?
Edit
On a similar note, when I try to set default values, i.e., std::string b = "b" in all code (updated below), the Rcpp-module-versions do not work when trying to use the default value (foo1(a = "a") results in Error Expecting single string value and abc$foo3(a = "a") results in Error in ... could not find valid method)
test package
I have created the package with
Rcpp::Rcpp.package.skeleton("test", module = T)
then I added the two following files in test/
added src/foo1.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void foo1(std::string a, std::string b = "b") {
Rprintf("a is %s, b is %s", a.c_str(), b.c_str());
}
added src/ABC.cpp
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
#include <Rcpp.h>
void foo2(std::string a, std::string b = "b") {
Rprintf("a is %s; b is %s\n", a.c_str(), b.c_str());
}
class ABC {
public:
ABC() {}
void foo3(std::string a, std::string b = "b");
};
void ABC::foo3(std::string a, std::string b) {
Rprintf("a is %s; b is %s\n", a.c_str(), b.c_str());
}
RCPP_MODULE(ABCMODULE){
using namespace Rcpp ;
function("foo2" , &foo2 , "documentation for foo2");
// with formal arguments specification
class_<ABC>("ABC")
// expose the default constructor
.constructor()
.method("foo3", &ABC::foo3 , "documentation for foo3")
;
}
modified R/zzz.R
I added the following line to the bottom of the file
loadModule("ABCMODULE", TRUE)
I'm trying to replicate the following example from the mxnet main docs with mxnet.jl in Julia:
A = Variable('A')
B = Variable('B')
C = B * A
D = C + Constant(1)
# get gradient node.
gA, gB = D.grad(wrt=[A, B])
# compiles the gradient function.
f = compile([gA, gB])
grad_a, grad_b = f(A=np.ones(10), B=np.ones(10)*2)
The example shows how to autodiff a symoblic expression and obtain its gradients.
What is the equivalent in mxnet.jl (latest version 2016-03-07)?
Code in MXNet.jl/src/symbolic-node.jl may be helpful for you to find answers.
I am not familiar with this package.
Here is my Guess:
A = mx.Variable("A")
B = mx.Variable("B")
C = B .* A
D = C + 1
mx.normalized_gradient may be the solution to the remaining part if exists.
I have a Julia function in a file. Let's say it is the below. Now I want to pass arguments into this function. I tried doing
julia filename.jl randmatstat(5)
but this gives an error that '(' token is unexpected. Not sure what the solution would be. I am also a little torn on if there is a main function / how to write a full solution using Julia. For example what is the starting / entry point of a Julia Program?
function randmatstat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i = 1:t
a = randn(n,n)
b = randn(n,n)
c = randn(n,n)
d = randn(n,n)
P = [a b c d]
Q = [a b; c d]
v[i] = trace((P.'*P)^4)
w[i] = trace((Q.'*Q)^4)
end
std(v)/mean(v), std(w)/mean(w)
end
Julia doesn't have an "entry point" as such.
When you call julia myscript.jl from the terminal, you're essentially asking julia to execute the script and exit. As such, it needs to be a script. If all you have in your script is a function definition, then it won't do much unless you later call that function from your script.
As for arguments, if you call julia myscript.jl 1 2 3 4, all the remaining arguments (i.e. in this case, 1, 2, 3 and 4) become an array of strings with the special name ARGS. You can use this special variable to access the input arguments.
e.g. if you have a julia script which simply says:
# in julia mytest.jl
show(ARGS)
Then calling this from the linux terminal will give this result:
<bashprompt> $ julia mytest.jl 1 two "three and four"
UTF8String["1","two","three and four"]
EDIT: So, from what I understand from your program, you probably want to do something like this (note: in julia, the function needs to be defined before it's called).
# in file myscript.jl
function randmatstat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i = 1:t
a = randn(n,n)
b = randn(n,n)
c = randn(n,n)
d = randn(n,n)
P = [a b c d]
Q = [a b; c d]
v[i] = trace((P.'*P)^4)
w[i] = trace((Q.'*Q)^4)
end
std(v)/mean(v), std(w)/mean(w)
end
t = parse(Int64, ARGS[1])
(a,b) = randmatstat(t)
print("a is $a, and b is $b\n")
And then call this from your linux terminal like so:
julia myscript.jl 5
You can try running like so:
julia -L filename.jl -E 'randmatstat(5)'
Add the following to your Julia file:
### original file
function randmatstat...
...
end
### new stuff
if length(ARGS)>0
ret = eval(parse(join(ARGS," ")))
end
println(ret)
Now, you can run:
julia filename.jl "randmatstat(5)"
As attempted originally. Note the additional quotes added to make sure the parenthesis don't mess up the command.
Explanation: The ARGS variable is defined by Julia to hold the parameters to the command running the file. Since Julia is an interpreter, we can join these parameters to a string, parse it as Julia code, run it and print the result (the code corresponds to this description).