| #------------------------------------------------------------- |
| # |
| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance |
| # with the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, |
| # software distributed under the License is distributed on an |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| # KIND, either express or implied. See the License for the |
| # specific language governing permissions and limitations |
| # under the License. |
| # |
| #------------------------------------------------------------- |
| |
| # product between matrix and scalar |
| f1 = function (matrix[double] M, double factor) return (double res) { |
| res = prod(M) * factor |
| } |
| |
| # replace the matrix with a scalar and then use calculate the product |
| f2 = function (matrix[double] M, double r) return (double res) { |
| R = replace(target=M, pattern=1, replacement=r) |
| res = f1(R, 10) |
| } |
| |
| # production of two matrix |
| f3 = function (matrix[double] M1, matrix[double] M2) return (matrix[double] res) { |
| res = M1 %*% M2 |
| } |
| |
| f4 = function (matrix[double] M1, matrix[double] M2) return (matrix[double] res) { |
| res = M1 %*% M2 |
| } |
| |
| # some variables |
| X = matrix("1 2 3 4", rows=2, cols=2) |
| y = 10 |
| |
| R1 = eval("f1", X, y) |
| R2 = eval("f2", X, y) |
| R3 = eval("f3", X, X) |
| for(i in 3:4) |
| R4 = eval("f"+i, X, X); |
| |
| print(toString(R1)) |
| print(toString(R2)) |
| print(toString(R3)) |
| print(toString(R4)) |