MatrixMatrixMultiply error invalid arguments - linear-algebra

I am working on a procedure on Maple and it is generating a error. While i do the same steps outside the procedure and i don't get any error! (Error, (in LinearAlgebra:-Multiply) invalid arguments.
This is my Maple code:
Transform := proc (A, T)
local TI, TD, M, N, K, R, B;
TI := MatrixInverse(T, method = pseudo);
TD := Map(diff, T, x);
M := MatrixMatrixMultiply(A, T);
N := MatrixMatrixMultiply(TI, M);
K := MatrixMatrixMultiply(TI, TD);
R := N-K; B := Map(simplify, R, x);
RETURN B
end proc
Thanks forward for your help!

Make the second line of your procedure
uses LinearAlgebra;

Related

How to print in if statement in SML

This is the code I tried
fun printsl([], k) = true
| printsl(h::t) = if k > h then print(h) andalso printsl(t);
But when I run the code, i get the following error
= stdIn:4.68-7.8 Error: syntax error: deleting SEMICOLON ID
stdIn:8.1 Error: syntax error found at EOF
The goal of the function is to print any number in the list that is less than the value k
A few things wrong here. Let's start with your function signature.
On the first line, your function takes 2 parameters, an empty list and whatever the type of k is (it is not important yet). Then on the second line, the function takes just one parameter, a non-empty list.
The two lines should match to look like:
fun printsl([], k) = ...
| printsl(h::t, k) = ...
Now let's think about the use of andalso. andalso is an operator which takes two booleans and returns a bool. It can be considered to have the signature bool * bool -> bool.
Your usage print(h) andalso printsl(t) does not match this signature.
The type of print is string -> unit, so the type of print(h) is unit (assuming h to be a string). As such, the usage of andalso is incorrect as the types on each side are not bools.
Instead of using andalso, we can simply execute both statements (print(h); printsl(t, k)). Sequences like this are expressions which return the last value. That is to say (x; y; z) returns z.
fun printsl([], k) = true
| printsl(h::t, k) = if h < k then (print(h); printsl(t, k));
However, this is still broken as the if-else construct in SML is an expression and must have a matching else, so you could use either of the following:
fun printsl([], k) = true
| printsl(h::t, k) =
if h < k then (print(h); printsl(t))
else printsl(t, k);
fun printsl([], k) = true
| printsl(h::t, k) = (
if h < k then print(h) else ();
printsl(t, k)
);
I personally prefer the latter as it prevents repetition of printsl.
This code will compile, but the signature is wrong. As we use h directly as a parameter of print, its type is inferred to be a string. This means that the compiler determines printsl to have type string list * string -> bool, whereas we are aiming for int list * int -> bool.
This can be corrected by changing the call print(h) to print(Int.toString h), giving us:
fun printsl([], k) = true
| printsl(h::t, k) = (
if h < k then print(Int.toString h) else ();
printsl(t, k)
);
This is now a function which will print all values in the given list which are less than k, but it always returns true. This provides no extra information so I would be inclined to change the signature to int list * int -> unit, giving us (finally):
fun printsl([], k) = ()
| printsl(h::t, k) = (
if h < k then print(Int.toString h) else ();
printsl(t, k)
);
This entire program could also be written in a more functional manner using List.app and List.filter.
fun printsl (xs, k) =
List.app
(fn y => print (Int.toString y))
(List.filter
(fn x => x < k)
xs);

substitute list of equations into expression

In the following toy problem I'd like to evaluate j(fxxx) but with with the actual numbers for a,b,c that solve produces, is there a way to do that? According to the docs there is subst but it takes three arguments (variables to be substituted, the values, and the expression).
display2d:false;
i(f) := integrate(f(x),x,0,1);
j(f) := a*f(0)+b*f(1/2)+c*f(1);
f1(x) := 1;
fx(x) := x;
fxx(x) := x^2;
fxxx(x) := x^3;
print(solve([i(f1) = j(f1), i(fx) = j(fx), i(fxx) = j(fxx)],[a,b,c]));
Try it online!
Actually I should have finished reading the docs, we can actually still use subst:
i(f) := integrate(f(x),x,0,1);
j(f) := a*f(0)+b*f(1/2)+c*f(1);
f1(x) := 1;
fx(x) := x;
fxx(x) := x^2;
fxxx(x) := x^3;
jj(g) := subst(solve([i(f1) = j(f1), i(fx) = j(fx), i(fxx) = j(fxx)],[a,b,c]),j(g));
print(jj(g));
print(jj(fxxx)-i(fxxx));
Try it online!

Assign result of reflect.AppendSlice to pointer

I have troubles translating this piece of code, which is effectively a left rotate on a slice, into a more generic version which accepts interface{} as an input parameter.
func rotate(a *[]int, i int) {
x, b := (*a)[:i], (*a)[i:]
*a = append(b, x...)
}
I have troubles with the final assignment:
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a)
x, b := v.Elem().Slice(0, i), v.Elem().Slice(i, v.Elem().Len())
*a = reflect.AppendSlice(b, x)
}
The error message is invalid indirect of a (type {}). The value of a is interface{}, hence *a = would be to assign the right-hand value to the space where the pointer is pointing to. My call to AppendSlice returns Value though. I am not sure where the type assertion needs to happen, I suppose on the left-hand side?
a is an interface{} not a pointer, so you can't dereference it. Even if you have a pointer to a slice, you can't assigned the result ofreflect.AppendSlice, because it returns the type reflect.Value. You need to set the value via Value.Set.
https://play.golang.org/p/JCF8jsRJ_O
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a).Elem()
x, b := v.Slice(0, i), v.Slice(i, v.Len())
v.Set(reflect.AppendSlice(b, x))
}
Eliminate the use of the reflect package in Go 1.18 and later by using generics:
func rotateSlice[T any](a []T, i int) []T {
x, b := a[:i], a[i:]
return append(b, x...)
}
Call it like this: x = rotateSlice(x, 2)
Here's now to implement rotate using the reflect package.
Use Value.Set to set the value in the slice.
func rotateSlice(a interface{}, i int) {
v := reflect.ValueOf(a).Elem()
x, b := v.Slice(0, i), v.Slice(i, v.Len())
v.Set(reflect.AppendSlice(b, x))
}
Call the function with a pointer to a slice:
a := []string{"a", "b", "c", "d"}
roateSlice(&a, 2)
Run all of the examples on the playground.

What does range or map return?

Go has very neat multiple return values paradigm. But It looks like v, ok := map[key] and v, k := range m use different mechanism with same notation. Here is a simple example:
func f2() (k, v string) {
return "Hello", "World"
}
func main(){
k := f2() // Doesn't work : multiple-value f2() in single-value context
m := map[string]int{"One": 1}
// It works
v, ok := m["One"]
// How it all work?
v := m["One"]
for k := range m {}
}
In above example, k := f2() gives error as f2 returns two values, whereas v, ok := m["One"] and v := m["One"] - both expressions work without any error.
Why is that different behavior?
A fetch from the built in map, using range on a map, array or slice, and also type assertions allows for one or two variables. This is not the case for user defined functions and methods. If a function declares two return values, you must tell what to do with both of them, or ignore both:
k, _ := f2() // Specify what to do with each returned value
f2() // Ignoring both
Why? Because the specification says it is so:
Map (indexed expressions):
An index expression on a map a of type map[K]V may be used in an assignment or initialization of the special form
v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]
where the result of the index expression is a pair of values with types (V, bool). In this form, the value of ok is true if the key x is present in the map, and false otherwise. The value of v is the value a[x] as in the single-result form.
Range (for statement):
For each iteration, iteration values are produced as follows:
Range expression: m map[K]V
1st value: key k K
2nd value (if 2nd variable is present): m[k] V
Type assertion:
For an expression x of interface type and a type T, the primary expression
x.(T)
asserts that x is not nil and that the value stored in x is of type T.
and
If a type assertion is used in an assignment or initialization of the form
v, ok = x.(T)
v, ok := x.(T)
var v, ok = x.(T)
the result of the assertion is a pair of values with types (T, bool)

Maple: Why I can not plot my function which is definrd by proc?

So I created a proc that returns a value. (sqrt analog that is correct for numbers from (2.1) and up). I can evaluate it for any given number but I cannot plot it. Why and how to fix it?
code (converted to 1-d math input):
>
restart:
with(plottools):
val := 248;
sqr := proc (sqrtFrom, iterations) answer := 0; ampl := 0; number := sqrtFrom; for k to iterations do answer := 10*answer; number := sqrtFrom*10^ampl; for i from 0 while i < number do answer := answer+1; i := answer^2 end do; answer := answer-1; difr := -1; while difr < 0 do ampl := ampl+1; difr := sqrtFrom*10^ampl-100*answer^2 end do end do; return evalf(answer/10^(iterations-1)) end proc;
>
evalf(sqrt(val));
sqr(val, 10);
plot(sqr(x, 10), x = 3 .. 5);
>
You need eval quotes. Try
plot('sqr'(x,10), x = 3..5 );
The error you get is because sqr is being called prematurely with x as an argument, and it can't do that.
Alternatively, you can modify sqr itself to return unevaluated if it gets non-numeric arguments (which is how sqrt works).

Resources