Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:17:49+00:00 2026-06-15T17:17:49+00:00

I get a weird effect when trying to overload the + operator and using

  • 0

I get a weird effect when trying to overload the + operator and using the Matrix package with sparse matrices. I first define a very simple class that does not use the Matrix package but has a + operator. I then sum two sparse matrices. The first M+M addition delivers the expected result but the second throws an error. Here is a very simple code that generates the error:

require(Matrix)
setClass("TestM",representation(M='numeric'))
setMethod("initialize", "TestM", function(.Object,x) { 
  .Object@M = x
  .Object
})
setMethod("+", c("TestM","TestM"), function(e1,e2) {
  e1@M + e2@M
})

M = Matrix(diag(1:10),sparse=T)
M+M  # > FINE
M+M  # > ERROR

M = Matrix(diag(1:10),sparse=F)
M+M  # > FINE
M+M  # > FINE

The second addition throws the following error:

Error in forceSymmetric(callGeneric(as(e1, "dgCMatrix"), as(e2, "dgCMatrix"))) : 
  error in evaluating the argument 'x' in selecting a method for function
'forceSymmetric': Error in .Arith.Csparse(e1, e2, .Generic, class. = "dgCMatrix") :
  object '.Generic' not found

And the error does not happen if the matrices are not sparse. Is there some interference between the + I define and the + for sparseMatrix ? Do I not define the + operator correctly?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:17:50+00:00Added an answer on June 15, 2026 at 5:17 pm

    Try setting the Ops class to be overloaded:

    > setMethod(Ops, c("TestM","TestM"), function(e1,e2) {
    +   e1@M + e2@M
    + })
    [1] "Ops"
    attr(,"package")
    [1] "base"
    > 
    > M = Matrix(diag(1:10),sparse=T)
    > M+M  # > FINE
    10 x 10 sparse Matrix of class "dsCMatrix"
    
     [1,] 2 . . .  .  .  .  .  .  .
     [2,] . 4 . .  .  .  .  .  .  .
     [3,] . . 6 .  .  .  .  .  .  .
     [4,] . . . 8  .  .  .  .  .  .
     [5,] . . . . 10  .  .  .  .  .
     [6,] . . . .  . 12  .  .  .  .
     [7,] . . . .  .  . 14  .  .  .
     [8,] . . . .  .  .  . 16  .  .
     [9,] . . . .  .  .  .  . 18  .
    [10,] . . . .  .  .  .  .  . 20
    > M+M  # (NOT error)... was  ERROR
    10 x 10 sparse Matrix of class "dsCMatrix"
    
     [1,] 2 . . .  .  .  .  .  .  .
     [2,] . 4 . .  .  .  .  .  .  .
     [3,] . . 6 .  .  .  .  .  .  .
     [4,] . . . 8  .  .  .  .  .  .
     [5,] . . . . 10  .  .  .  .  .
     [6,] . . . .  . 12  .  .  .  .
     [7,] . . . .  .  . 14  .  .  .
     [8,] . . . .  .  .  . 16  .  .
     [9,] . . . .  .  .  .  . 18  .
    [10,] . . . .  .  .  .  .  . 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement some simple tooltip (TipTip) effect, but for some weird
I'm trying to use Asp.NET WebAPI module but I get a weird error. When
i'm trying to get started with silverlight 2. i have a weird bug. every
I've a weird string escape problem with my PHP script. I'm trying to get
I am pulling in an RSS feed using PHP. I get weird characters in
I am using the jQuery UI to get a UI effect something like drag
I get some weird overflow whenever I want to fit an object inside foreignObject
I get a weird problem. Getting java.util.ConcurrentModificationException . But I'm not modifying the current
Hello I get a weird error in this code: function validateForm(aform) { var s=;
when i use connection.queries in django i get a weird exception: 'list' object is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.