I am trying to write the following function using functional programming.
def mv(A,X,n):
Y = [0]*n
for i in range(n):
for j in range(n):
Y[i] += A[i][j] * X[j]
return Y
Can the reduce function multiply a vector with a matrix? Because that is what I am trying to do here. I am trying to multiply the matrix A
with the vector X
but receive an error. The error is:
TypeError: 'function' object is not iterable
def mv(A,X,n):
return list(map(sum,lambda x, y: x*y(A, X)))
To the input:
A = [[1,2],[3,4]]
X = [1,2]
n = 2
print(mv(A,X,n))
The output should be:
[5,11]
答案 0 :(得分:1)
Here's a quick and dirty solution using list-comprehensions, if you want to stick with functional constructs (comprehensions being favored in Python over @JsonCreator
public Location(@JsonProperty("A") String a,
@JsonProperty("B") int b,
@JsonProperty("C") int b,
@JsonProperty("D") int d,
int newFiled) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.newFiled = newFiled;
}
public int newFiled() {
return a+b+c;
}
Error:
} 2017-12-18 12:53:24,194 WARN o.s.h.c.j.MappingJackson2HttpMessageConverter [I/O dispatcher 3] Failed to evaluate deserialization for type [simple type, class com.gap.plan.allocati
fasterxml.jackson.databind.JsonMappingException: Argument #4 of constructor [constructor for com.gap.plan.allocation.model.SkuInventory$DCLocation, annota
tions: {interface com.fasterxml.jackson.annotation.JsonIgnoreProperties=@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown=true, allowGetters=false, value=[], allow
Setters=false), interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name w
hen multiple-parameter constructor annotated as Creator
{} 2017-12-18 12:53:24,196 ERROR c.g.p.a.s.ProductAllocationService [ForkJoinPool-2-worker-36] Unknown processing error
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.gap.plan.allocation.model.SkuInven
tory] and content type [application/json;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.convertResponse(AsyncRestTemplate.java:680) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.adapt(AsyncRestTemplate.java:667) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.adapt(AsyncRestTemplate.java:642) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.util.concurrent.FutureAdapter.adaptInternal(FutureAdapter.java:100) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.util.concurrent.ListenableFutureAdapter$1.onSuccess(ListenableFutureAdapter.java:56) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.util.concurrent.ListenableFutureCallbackRegistry.success(ListenableFutureCallbackRegistry.java:118) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.http.client.HttpComponentsAsyncClientHttpRequest$HttpResponseFutureCallback.completed(HttpComponentsAsyncClientHttpRequest.java:121) ~[spring-web-4.2.4
.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.http.client.HttpComponentsAsyn c
ClientHttpRequest$HttpResponseFutureCallback.completed(HttpComponentsAsyncClientHttpRequest.java:102) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177) ~[httpasyncclient-4.1.1.jar:4.1.1]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:432) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:325) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:267) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) ~[httpasyncclient-4.1.1.jar:4.1.1]
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) ~[httpasyncclient-4.1.1.jar:4.1.1]
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:123) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:164) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:339) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:317) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:278) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106) ~[httpcore-nio-4.4.4.jar:4.4.4]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:590) ~[httpcore-nio-4.4.4.jar:4.4.4]
at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_131]
and map
):
filter
To see in action:
import operator as op
x = [[1],[2],[3]] # Notice the dimensions!
A = [[1,1,1],
[2,2,2],
[3,3,3]]
def matrix_mult(A, X):
return [
[sum(map(op.mul, row, col)) for col in zip(*X)]
for row in A
]