| #------------------------------------------------------------- |
| # |
| # 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. |
| # |
| #------------------------------------------------------------- |
| |
| foo = function(Matrix[Double] weights, Matrix[Double] X, Integer p, Integer P, Integer q, Integer Q, Integer s) return(matrix[double] grad) { |
| combined_weights = rbind (weights, matrix(2, p*P, 1)) |
| res_A = matrix(1, rows=p+P, cols=1) |
| grad = matrix(0, rows=p+P, cols=1) |
| if (p > 0) grad[1:p,] = res_A[1:p,] |
| if (P > 0) grad[p+1:p+P,] = res_A[p+1:p+P,] |
| if (p>0 & P>0){ |
| res_A = res_A[p+P+1:nrow(res_A),] |
| for(i in seq(1, p, 1)){ |
| permut = matrix(0, rows=p, cols=P) |
| permut[i,] = t(combined_weights[p+1:p+P,]) |
| grad[i,1] = grad[i,1] + sum(res_A * matrix(permut, rows=p*P, cols=1)) |
| } |
| for(i in seq(1, P, 1)){ |
| permut = matrix(0, rows=p, cols=P) |
| permut[,i] = combined_weights[1:p,] |
| grad[p+i,1] = grad[p+i,1] + sum(res_A * matrix(permut, rows=p*P, cols=1)) |
| } |
| } |
| } |
| |
| best_point = eval ("foo", matrix(1, 2, 1),matrix(0, 998, 3), 2, 0, 0, 0, 10) |
| print(toString(best_point)) |