fun make_alt_record (stringlists, {x, y, z}) =
case generate_alts (stringlists, x) of
[] => {x, y, z}
| alt::stringlists' => {name, y, z}
generate_alts consumes a string list list and a string, and produces a string list.
make_alt_record is supposed to generate a new record.
Instead it generates errors, which are as follows:
hw2provided.sml:48.6-48.8 Error: syntax error: replacing DARROW with LPAREN
hw2provided.sml:49.23-49.25 Error: syntax error: replacing DARROW with AS
hw2provided.sml:65.1 Error: syntax error found at EOF
Your record syntax is wrong. You can pattern match on records, but you can't create them by punning. You need, e.g.
[] => {x = x, y = y, z = z}
| ... => {x = name, y = y, z = z}
Related
I would like to use my calculated y[0] (x-values) and y[1] (y-values) from sol_3 using the solve_ivp to find the radius(r) and plot r(t).
How can I calculate r using x and y values from sol_r? I keep getting TypeError: only size-1 arrays can be converted to Python scalars.
It seems like I am having issues with data type.
Please find my code below:
timing = np.array([0,2*math.pi]) #t0 = 0 tf=2pi
sol_3 = solve_ivp(fun = motion, t_span=[-math.pi, 2*math.pi], y0 = initial, method='RK23')
x = (sol_3.y[0, :])
y = (sol_3.y[1, :])
r = math.sqrt(sol_3.y[1,:]**2+sol_3.y[0,:]**2)
The array of the sol_3 looks like this:
message: 'The solver successfully reached the end of the integration interval.'
nfev: 599
njev: 0
nlu: 0
sol: None
status: 0
success: True
t: array([-3.14159265, -3.14155739, -3.14120473, -3.13767817, -3.12223578,
-3.09595606, -3.0602884 , -3.01493539, -2.96220999, -2.90868487,
-2.86527118, -2.82204945, -2.76814683, -2.69499434, -2.61108567,
-2.51943819, -2.44000487, -2.36057156, -2.28852194, -2.20054302,
-2.10648128, -2.03001656, -1.97695204, -1.92388751, -1.87465566,
-1.82156056, -1.76673945, -1.73011416, -1.70585555, -1.68159695,
-1.64979671, -1.60847127, -1.55783888, -1.50388809, -1.45063319,
-1.40126952, -1.3488944 , -1.27797028, -1.19501887, -1.1039372 ,
-1.02086808, -0.95113058, -0.88636741, -0.80259334, -0.70932861,
-0.61584263, -0.55777683, -0.50936554, -0.46344206, -0.4113168 ,
-0.35663683, -0.31427459, -0.28317047, -0.25206634, -0.22135512,
-0.18102468, -0.13108896, -0.07689042, -0.02318517, 0.02738243,
0.07818198, 0.14701599, 0.22918967, 0.31978884, 0.41431515,
0.48493772, 0.55000025, 0.63412425, 0.72775963, 0.82162403,
0.87984573, 0.92849841, 0.97470164, 1.02711062, 1.0820909 ,
1.12462382, 1.15583674, 1.18704967, 1.21796912, 1.25856295,
1.30880593, 1.36330716, 1.41729713, 1.46809564, 1.51919004,
1.58841181, 1.67102044, 1.76206092, 1.85696013, 1.92801041,
I'm initializing an array of record which also contains a string. I'm getting an error HDLCompiler:806 Line 109: Syntax error near "text_passages" (Last line in the code below). What's the correct way of initialization?
type text_info is
record
text : string(1 to 15);
x: integer;
y: integer;
end record;
constant init_text_info: text_info := (text => " ", x => 0, y => 0);
type text_info_array is array(natural range <>) of text_info;
My declaration and initialization is as follows
signal text_passages : text_info_array(0 to 1) := (others => init_text_info);
text_passages(0) <= (text => "This is a Test.", x => 50, y => 50);
Well, you've got an extra bracket at the end of your last line, but apart from that, it's fine. (I doubt the error message you report is caused by that bracket.) The last line should be:
text_passages(0) <= (text => "This is a Test.", x => 50, y => 50);
An [MCVE]:
entity E is
end entity ;
architecture A of E is
type text_info is
record
text : string(1 to 15);
x: integer;
y: integer;
end record;
constant init_text_info: text_info := (text => " ", x => 0, y => 0);
type text_info_array is array(natural range <>) of text_info;
signal text_passages : text_info_array(0 to 1) := (others => init_text_info);
begin
text_passages(0) <= (text => "This is a Test.", x => 50, y => 50);
end architecture A;
https://www.edaplayground.com/x/4ARJ
(It's always best to submit an MCVE.)
I am using rscala to communicate Scala and R. Lets say I have a Scala function that returns an Array of Array[Double] and a Double as:
Array(projectedTrainToMatrix,predictedTrain,kernelParam,projection,thresholdsToArray)
where kernelParam is of type Double and the others are Array[Double]. When I run the method from R as:
myfit<-s$.cristinahg.ocapis.kdlor$kdlorfit(traindata,trainlabels,kerneltype,params)
I get myfit as
ScalaCachedReference... _: Array[Array[_]]
[Ljava.lang.Object;#b9dfc5a
But I want to access each of the values in myfitArray. I have tried to access them via myfit$'(1)' but I get this instead the desired Array of Double:
function (..., .AS.REFERENCE = NA, .EVALUATE = TRUE, .PARENTHESES = FALSE)
{
args <- list(...)
if (!is.null(names(args)))
stop("Arguments should not have names.")
names <- paste0(rep("$", length(args)), seq_len(length(args)))
header <- mkHeader(args, names)
identifier <- ""
body <- if (inherits(reference, "ScalaInterpreterReference"))
paste0(reference[["identifier"]], ".", method)
else if (inherits(reference, "ScalaCachedReference")) {
if (.EVALUATE) {
identifier <- reference[["identifier"]]
paste0("R.cached($0).asInstanceOf[", reference[["type"]],
"].", method)
}
else {
paste0("R.cached(\\"", reference[["identifier"]],
"\\").asInstanceOf[", reference[["type"]], "].",
method)
}
}
else if (inherits(reference, "ScalaInterpreterItem")) {
if (method == "new")
paste0("new ", reference[["snippet"]])
else paste0(reference[["snippet"]], ".", method)
}
else stop("Unrecognized reference type.")
argsList <- paste0(names, collapse = ",")
if ((nchar(argsList) > 0) || .PARENTHESES)
argsList <- paste0("(", argsList, ")")
snippet <- paste0(header, paste0(body, argsList))
if (get("show.snippet", envir = interpreter[["env"]]))
cat("<<<\\n", snippet, "\\n>>>\\n", sep = "")
cc(interpreter)
wb(interpreter, DEF)
wc(interpreter, snippet)
flush(interpreter[["socketIn"]])
status <- rb(interpreter, "integer")
if (status != OK) {
if (get("serializeOutput", envir = interpreter[["env"]]))
echoResponseScala(interpreter)
stop("Problem defining function.")
}
functionName <- rc(interpreter)
if (get("serializeOutput", envir = interpreter[["env"]]))
echoResponseScala(interpreter)
f <- function(..., .NBACK = 1) {
args <- list(...)
if (length(args) != length(names))
stop("Incorrect number of arguments.")
if (!is.null(names(args)))
stop("Arguments should not have names.")
workspace <- new.env(parent = parent.frame(.NBACK))
assign(".rsI", interpreter, envir = workspace)
for (i in seq_len(length(args))) assign(names[i], args[[i]],
envir = workspace)
cc(interpreter)
wb(interpreter, INVOKE)
wc(interpreter, functionName)
wc(interpreter, identifier)
flush(interpreter[["socketIn"]])
rServe(interpreter, TRUE, workspace)
status <- rb(interpreter, "integer")
if (get("serializeOutput", envir = interpreter[["env"]]))
echoResponseScala(interpreter)
if (status != OK)
stop("Problem invoking function.")
result <- scalaGet(interpreter, "?", .AS.REFERENCE)
if (is.null(result))
invisible(result)
else result
}
if (.EVALUATE)
f(..., .NBACK = 2)
else f
}
<bytecode: 0x55b1ba98b3f8>
<environment: 0x55b1bd2616c0>
So how can I access each element of the Scala Array in R?
Your example shows that you are using rscala < 3.0.0. While it could be done in older versions, I recommend you use a recent version on CRAN. Below I provide a solution using rscala 3.1.0.
library(rscala)
scala()
s + '
def kdlorfit(
projectedTrainToMatrix: Array[Double], predictedTrain: Array[Double],
kernelParam: Double, projection: Array[Double], thresholdsToArray: Array[Double]) =
{
Array(projectedTrainToMatrix,predictedTrain,kernelParam,projection,thresholdsToArray)
}
'
x1 <- c(1,2,3)
x2 <- c(11,12,13)
x3 <- 34
x4 <- c(100,110,120)
x5 <- c(50,51)
myfit <- s$kdlorfit(x1,x2,x3,x4,x5)
scalaType(myfit)
identical(x1,myfit(0L)$"asInstanceOf[Array[Double]]"())
identical(x3,myfit(2L)$"asInstanceOf[Double]"())
Note the need to cast using asInstanceOf because the Scala type of myfit is Array[Any].
If the function returned Array[Array[Double]] instead of Array[Any], no casting would be needed, as shown below.
s + '
def kdlorfit2(
projectedTrainToMatrix: Array[Double],
predictedTrain: Array[Double],
kernelParam: Array[Double],
projection: Array[Double],
thresholdsToArray: Array[Double]) =
{
Array(projectedTrainToMatrix,predictedTrain,kernelParam,projection,thresholdsToArray)
}
'
myfit <- s$kdlorfit2(x1,x2,I(x3),x4,x5)
scalaType(myfit)
identical(x1,myfit(0L))
identical(x3,myfit(2L))
Note that, when calling kdlorfit2, the argument x3 is passed as Array[Double] because it is wrapped in I(). Without wrapping, it is a passed as a Double as in the previous example.
I'm reading data from file to create edges of graph. I've declare an array and adding edges in it one by one. This code is working fine:
class AIRecipes()
case class edgeProperty(val relation: String, val usedIn: String) extends AIRecipes
var edgeArray = Array(Edge(0L, 0L, edgeProperty("", "")))
edgeArray = edgeArray ++ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )
But in first line, instead of declaring an extra edge with dummy values, I want to declare an empty array like that:
var edgeArray = Array.empty[Edge[(Long, Long, Object)]]
edgeArray = edgeArray ++ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )
But it give me following compilation error on '++':
type mismatch; found : Array[org.apache.spark.graphx.Edge[_ >:
(Long, Long, Object) with
net.sansa_stack.template.spark.rdf.TripleReader.edgeProperty <:
Product with Serializable]] required:
Array[org.apache.spark.graphx.Edge[(Long, Long, Object)]] Note:
org.apache.spark.graphx.Edge[_ >: (Long, Long, Object) with
net.sansa_stack.template.spark.rdf.TripleReader.edgeProperty <:
Product with Serializable] >: org.apache.spark.graphx.Edge[(Long,
Long, Object)], but class Array is invariant in type T. You may wish
to investigate a wildcard type such as _ >:
org.apache.spark.graphx.Edge[(Long, Long, Object)].
I also tried this:
edgeArray :+ Array(Edge(VertexId, VertexId, edgeProperty("", "")) )
It don't give me compilation error but nothing is being added in array.
Type of the first array is incorrect. Note that Edge is parameterized only by its property so the type of the expression you are trying to merge is Array[Edge[edgeProperty]]:
scala> :t Array(Edge(0L, 0L, edgeProperty("", "")))
Array[org.apache.spark.graphx.Edge[edgeProperty]]
while you define variable as Array.empty[Edge[(Long, Long, Object)]].
The Object part is the second problem. As you can read in the exception message, Array (as any other mutable container) is invariant. So if you really want to go with Object you'll have to:
scala> var edgeArray = Array.empty[Edge[Object]]
edgeArray: Array[org.apache.spark.graphx.Edge[Object]] = Array()
scala> edgeArray = edgeArray ++ (Array(Edge(1L, 2L, edgeProperty("", "")) ): Array[Edge[Object]])
edgeArray: Array[org.apache.spark.graphx.Edge[Object]] = [Lorg.apache.spark.graphx.Edge;#338
but I still recommend
scala> var edgeArray = Array.empty[Edge[edgeProperty]]
edgeArray: Array[org.apache.spark.graphx.Edge[edgeProperty]] = Array()
scala> edgeArray = edgeArray ++ Array(Edge(1L, 2L, edgeProperty("", "")) )
edgeArray: Array[org.apache.spark.graphx.Edge[edgeProperty]] = [Lorg.apache.spark.graphx.Edge;#7d59e8d4
I am having difficulty in what should be a trivial task of creating an interface array. Here is my code,
var result float64
for i := 0; i < len(diff); i++ {
result += diff[i]
}
result = 1 / (1 + math.Sqrt(result))
id1 := user1.UserId
id2 := user2.UserId
user1.Similar[id2] = [2]interface{id2, result}
user2.Similar[id1] = [2]interface{id1, result}
result is a float and user*.UserId is an int.
My error message is
syntax error: name list not allowed in interface type
For example,
package main
import (
"fmt"
)
func main() {
x, y := 1, "#"
a := [2]interface{}{x, y}
fmt.Println(a)
b := [2]interface{}{0, "x"}
fmt.Println(b)
}
Output:
[1 #]
[0 x]