multiplication

Compute the product of the next n elements in array

天大地大妈咪最大 提交于 2019-12-05 16:36:50
I would like to compute the product of the next n adjacent elements of a matrix. The number n of elements to be multiplied should be given in function's input. For example for this input I should compute the product of every 3 consecutive elements, starting from the first. [p, ind] = max_product([1 2 2 1 3 1],3); This gives [1*2*2, 2*2*1, 2*1*3, 1*3*1] = [4,4,6,3] . Is there any practical way to do it? Now I do this using: for ii = 1:(length(v)-2) p = prod(v(ii:ii+n-1)); end where v is the input vector and n is the number of elements to be multiplied. in this example n=3 but can take any

Multiplying using Bitwise Operators

南楼画角 提交于 2019-12-05 04:45:34
问题 I was wondering how I could go about multiplying a series of binary bits using bitwise operators. However, I'm interested in doing this to find the a decimal fraction value for the binary value. Here's an example of what I'm trying to do: Given, say: 1010010, I want to use each individual bit so that it will be computed as: 1*(2^-1) + 0*(2^-2) + 1*(2^-3) + 0*(2^-4)..... Though I'm interested in doing this in ARM assembly, having an example in C/C++ will still help as well. I was thinking of

Divide and Conquer Matrix Multiplication

半世苍凉 提交于 2019-12-05 04:15:40
I am having trouble getting divide and conquer matrix multiplication to work. From what I understand, you split the matrices of size nxn into quadrants (each quadrant is n/2) and then you do: C11 = A11⋅ B11 + A12 ⋅ B21 C12 = A11⋅ B12 + A12 ⋅ B22 C21 = A21 ⋅ B11 + A22 ⋅ B21 C22 = A21 ⋅ B12 + A22 ⋅ B22 My output for divide and conquer is really large and I'm having trouble figuring out the problem as I am not very good with recursion. example output: Original Matrix A: 4 0 4 3 5 4 0 4 4 0 4 0 4 1 1 1 A x A Classical: 44 3 35 15 56 20 24 35 32 0 32 12 29 5 21 17 Divide and Conquer: 992 24 632 408

Variables Multiplication

半腔热情 提交于 2019-12-04 22:15:34
I'm making a script which gives the factorial for a inserted number, but i'm having some problems with the multiplication. Note: the factorial for is given by: 9!=9*8*7*6*5*4*3*2*1 Here's my code: #!/bin/bash echo "Insert an Integer" read input if ! [[ "$input" =~ ^[0-9]+$ ]] ; then exec >&2; echo "Error: You didn't enter an integer"; exit 1 fi function factorial { while [ "$input" != 1 ]; do result=$(($result * $input)) input=$(($input-1)) done } factorial echo "The Factorial of " $input "is" $result it keeps giving me errors of all kinds for diferent multiplication technics :/ Currently the

How to calculate EditText value in Android?

北城余情 提交于 2019-12-04 19:16:30
In an Android app, I'm using two EditText controls and multiplying their two values. If one EditText is null and in the second one I put a value, it's not working properly. How can I deal with this case, in which I have a value in one EditText and a null in the other and I want to multiply the two values? First of all, you need to have a trigger for when to perform the calculation. Say it's a button, or, even better, every time the value of one of your EditText s changes: private EditText editText1, editText2; private TextView resultsText; ............................... // Obtains references

does this condition suffice for overflow check in multiplication [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-04 17:59:05
This question already has an answer here: Catch and compute overflow during multiplication of two large integers 11 answers int isOverflow(uint a, uint b) { // a and b are unsigned non-zero integers. uint c = a * b; if (c < ( a > b ? a : b)) return 1; else return 0; } Am I missing something ? I think the above snippet will work. EDIT : I have seen other solutions like multiplication of large numbers, how to catch overflow which uses some fancy methods to check it. But to me above simple solution also looks correct. Thats why I am asking this question. ams It's easy to prove this is wrong by

Why does (int)(33.46639 * 1000000) return 33466389?

笑着哭i 提交于 2019-12-04 17:03:49
问题 (int)(33.46639 * 1000000) returns 33466389 Why does this happen? 回答1: Floating point math isn't perfect. What every programmer should know about it. Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Almost every language has a floating-point datatype; computers from PCs to supercomputers have floating-point accelerators; most compilers will be called upon to compile floating-point

Ruby - Multiplication issue

人盡茶涼 提交于 2019-12-04 09:55:30
My output is like this - ruby-1.9.2-p290 :011 > 2.32 * 3 => 6.959999999999999 And I remember sometime back on another machine I had got it like.. 2.32 * 3 = 6 What is my mistake? Thanks a ton for reading this. :) If you really want to round down to an integer then just (3 * 2.32).to_i but I think that's unlikely. Usually you just want to format the slightly imprecise floating point number to something like this "%0.2f" % (3 * 2.32) => "6.96" If you really want to work with the exact representation then you can use BigDecimal . require 'BigDecimal' (3 * BigDecimal.new("2.32")).to_s("F") => "6

Floating-point division in bash

孤者浪人 提交于 2019-12-04 06:25:20
问题 I'm trying to convert whatever numbers the user inputs into 2 decimal places. For instance What is the total cost in cents? 2345 output: 23.45 this is the code i have so far percentage=20 #cannot change numerical value must convert into 0.20 echo -n "What is the total cost? "; read cost_in_cents echo "scale 1; $cost_in_cents" | bc I'm also going to be doing some multiplication with percentage, how can i also convert the percentage into a float (0.20) 回答1: Perhaps it's nostalgia for reverse

Matrix multiplication in R: requires numeric/complex matrix/vector arguments

守給你的承諾、 提交于 2019-12-04 04:09:17
问题 I'm using the dataset BreastCancer in the mlbench package, and I am trying to do the following matrix multiplication as a part of logistic regression. I got the features in the first 10 columns, and create a vector of parameters called theta: X <- BreastCancer[, 1:10] theta <- data.frame(rep(1, 10)) Then I did the following matrix multiplication: constant <- as.matrix(X) %*% as.vector(theta[, 1]) However, I got the following error: Error in as.matrix(X) %*% as.vector(theta[, 1]) : requires